score:0
instead of put code in componentdidmount() try to put it in the constructor() or in the componentwillmount().
i recommend to put that code in the end of the constructor() method because componentwillmount will be deprecated in next versions of react native
score:1
asyncstorage
is an asynchronous function as the name suggests, so you have to wait for the response, that's why state has nothing because asyncstorage hasn's finished.
try {
const value = await asyncstorage.getitem('@userprofiledocid');
this.setstate({ userid: value})
} catch (error) {
// error retrieving data
}
and in my honest opinion you should use componentdidmount
it is safer but it's my opinion
see this for more opinions
calling setstate in componentwillmount is dangerous because you take a risk in calling setstate while rendering
Source: stackoverflow.com
Related Query
- React Native get initial state from async componentDidMount
- react native state changing problem in picker
- How can I get my firebase listener to load data to my redux state in a React Native app so I can read the data within my ComponentDidMount function?
- ComponentDidMount react native state
- Problem on changing my nested state React Native
- React native useEffect render problem when trying to get user's auth state firebase
- Set State Problem For Reusable Component React Native
- Problem with updating state in react native component
- Problem changing state value in react native with result of filter method
- Problem with redux auth state in react native
- React Native apply array values from state as Picker items
- what is difference between componentDidMount and componentDidUpdate in react native
- React Native Android, several apps instances running at the same time as result of coming from a background state without have killed the app before
- React Native ListView row not re-rendering after state change
- Deep linking not working when app is in background state React native
- React Native Pagination and React Navigation Problem
- react native AppState has no 'inactive' state on Android
- Pass state from child to parent component in React Native
- react native android: How to save state on screen rotation or orientation
- How to make a react native input which gives validation state feedback to the user. [Valid, Printine, Error, Editing]
- React native state change transitions
- React native deep linking not working when app is in background state React native
- Problem with "react-native-vector-icons" in React native
- Problem when installing pods after updating React Native
- React Native Access State from StyleSheet
- React native redux map state to props not working
- Show a tab bar item or not based on Redux state in React Native
- Update an element in an array object at state by using textInput React Native
- how to add an event listener to a state in react native
- React Native : use a variable string in the state like this : this.state.variableString
More Query from same tag
- React Native Reanimated: Argument of type 'AnimatedNode<number>' is not assignable to parameter of type 'number'.ts(2345)
- Image Background in Reactnative
- How to remove property when window is scaled to lower/higher size
- How to re-render componenets in react-native?
- How to pass values to array in react native
- React Native how to use styles props
- Error 404 when using GITHUB when I reload the page
- How to go from top to bottom of a ScrollView in React Native?
- How to set Marker location to users current location onInit, using react-native-maps and Marker Library
- How to close the razorpay webview after callback_url success response in react-native?
- I get a running error with the idex bundle or metro what to do?
- Graphql query error ! variable is declared but never used
- React Native Touchable on press down event
- React native npm start shows a big logo
- value.match is undefined in React
- React Native PanResponder
- How do I implement money transfer via paypal in react native?
- Map through firebase firestore collectiongroup, Not working
- How to test the value of TextInput in react-native
- Reload ListView not Working React Native
- Custom navBar using react-native-router-flux
- I can't change the screen
- How can I make a textField in react native required
- React native UI Kitten causes slow start up
- React not state not updating first time
- React Native + Redux Form - Use keyboard next button to go to next TextInput field
- App exit on back click on android in react native?
- React Native: Where exactly does it want the enclosing tags?
- Picker onValueChange() called twice
- React Native - VirtualizedLists should never be nested inside plain ScrollViews with the same orientation