last day (50 days later) » 

12:10 AM
-1
A: React native - is there a way to use an object from another page?

Somangshu GoswamiThe best way would be use redux store which helps you create immutable data object which can be update only based on user action https://redux.js.org/basics/store/. Another simple but not efficient way is to use the react native async storage, Where you store the object and later load it up in t...

 
user12877543
i am new in react native, can you show me how to fix my example to make it work ?
 
Sure, I show you the async store method for convenience, Will update my answer in a bit!
Hey added the solution, let me know if you still face problems implementing this
 
user12877543
Could you please show me your example on my code? Because I didn't really understand you and I want to see how to do it on my code.
 
user12877543
I try to do your solution but it's a little bit confuse me.
 
I'd disagree about Redux here, complete overkill.
 
12:10 AM
@James i agree to a situation like this its definetly an overkill
@miraclark i updated my answer to show how it fits your code. Let me know if you still face problems
 
@SomangshuGoswami but you've suggested this is the "best" approach, as it stands your answer is offering two overly complicated suggestions e.g. async storage would consist of unnessessary storage of the data and then having to re-read from disk.
 
user12877543
it says : referenceerror - cant find variable : storeService
 
@James with all due respect this is an approach which is valid and solves the problem. The storage here is not unnecessary and can also save a lot of network calls in many use case. You approach does the work, But that dosent mean you down-vote my answer.
@miraclark you need to import the service import storeService from ./storeService
 
@SomangshuGoswami I've explained my reasoning for downvoting quite clearly, I'd actually suggest simply keeping the state higher up the tree and passing down props before I'd recommend implementing a full Redux store or using persistent storage just to share a bit of state between components. You are trying to crack a nut with a sledgehammer here...
 
user12877543
its not let me do import storeService from ./storeService its gave me error also
 
12:10 AM
@James also the fact that i never claimed this as the "best" approach as you might have thought or read. The solution also seems lengthy to you because i have actually utilized the code form the original question.
@miraclark your file path to storeService is not correct, Can you exactly show me what is the error you are getting
 
@SomangshuGoswami it's literally written as the first line in your answer "The best way would be use redux store"... I haven't mentioned anything about the length of your code.
 
user12877543
this is the error : " Unable to resolve "@react-native-community/async-storage" from "components\storeService.js""
 
user12877543
my project is with Expo-react native..maybe its give the error because i need import "AsyncStorage " from another place ?
 
user12877543
if i change the library import AsyncStorage from '@react-native-community/async-storage'; to import { AsyncStorage } from 'react-native'; its says that "cant find variable userPrincipalName "
 
user12877543
@ Somangshu Goswami i can console the res now..and i see it in the console but its said error on the "userPrincipalName" that stay in the Axios . the error said : cant find variable "userPrincipalName"
 
12:10 AM
@miraclark that is correct, Expo reference to the library could be different. Yes that mean the variable userPrincipalName is not yet set. Use a null checker before using the variable
ok, You need to first set the state variable and then access it. So in the constructor, Set state variable - {userPrincipalName: null} , then when you call the async storage loadKey function in the componentDidMount function, set the state again based on the value of res
 
user12877543
i edit the "MainScreen", please tell me what i missing and how i fix the "componentDidMount" as you said ?
 
Updated the answer for better clearity, Take a look
 
user12877543
now it said error : " undefined is not an object 'userPrincipalName.split '
 
user12877543
Thanks it works !!
 
Great, Could you please accept the answer/upvote. I have got a downvote for no reason
 
12:10 AM
@SomangshuGoswami ^ reasoning is up there as clear as day.
 
 
5 hours later…
4:49 AM
Redux indeed is the best way to create immutable object which can only update on user actions and consistently update all the related views. This save you a lot of time from solving bugs related to unnecessary mutation of objects. Dose'nt sound like an overkill anymore to me, Well of course in this case it could be too much to think about and do and hence my alternative of using async storage. In fact this solution would work if there is no relationship between two components.
which actually is a redux like functionality(redux persistent storage).
Downvote still does not seem like the right thing to me as it just your view(subjective in nature) and does not make it wrong in any ways.
 

  last day (50 days later) »