New record not listed after inserting a new record into the server database. I have added this code to display my records on my React Native page.
async componentDidMount(){ const url = "list-user"; const params = "user_id=" + this.state.user_id; Api.postData(url, params) .then(response => { this.setState({loading: false}); if (response.status == 1) { this.setState({ userData: response.data }) } }) } render(){ return( <SafeAreaView style={styles.container}> <UserHeaderBackNavigationBar {...this.props} title="User Data" /> <View style={styles.mainContainer}> {this.renderUser()} </View> </SafeAreaView> ) } renderUser=()=> { const items = []; for (item of this.state.userData) { items.push( <View style={{ flexDirection: 'row', paddingHorizontal: 25, paddingBottom: 10, paddingTop: 10, justifyContent: 'space-between' }} key={item.id}> <View style={{ flex: .2, paddingLeft:5 }}> <Text style={styles.planVisit}>{item.name}</Text> </View> <View style={{ flex: .4, paddingLeft:5 }}> <Text style={styles.planVisit}>{item.email}</Text> </View> </View> ); } return items; }
It listed only old records not listed new records. While I will close the app and open it again then it will display. I want to display a record while the page redirects to this page after insert data. How can I do it?
If you have no option for a page reload, then you can follow this code.
async componentDidMount(){ const { navigation } = this.props; this.focusListener = navigation.addListener('focus', () => { this.loadUserDetails() }) } componentWillUnmount() { this.focusListener(); } loadUserDetails = () => { (async()=>{ const url = "list-user"; const params = "user_id=" + this.state.user_id; Api.postData(url, params) .then(response => { this.setState({loading: false}); if (response.status == 1) { this.setState({ userData: response.data }) } }) })() }
Hope it will be helpful for you.
Question and answer is powered by AnsPress.io