I’m new to React Native and I decided to implement a mini Twitter app. But I am stuck somewhere. As you can see below, I have a component named Posty which contains a StackNavigator in it. The screens are PostScreen and NewPostScreen. When I click the icon in the header of the PostScreen screen, I can navigate to NewPostScreen to write a new tweet. When I write the tweet and click the button in NewPostScreen, it navigates back to PostScreen but my new tweet doesn’t show up. I want to make an API call again to load my new tweet.
I have read the documentation “Navigation lifecycle” of React Native (https://reactnavigation.org/docs/en/navigation-lifecycle.html). It says “Consider a stack navigator with screens A and B. After navigating to A, its componentDidMount is called. When pushing B, its componentDidMount is also called, but A remains mounted on the stack and its componentWillUnmount is therefore not called. When going back from B to A, componentWillUnmount of B is called, but componentDidMount of A is not because A remained mounted the whole time.”
Posty.js
import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import {createStackNavigator, createAppContainer} from 'react-navigation';
import PostScreen from './screens/PostScreen';
import NewPostScreen from './screens/NewPostScreen'
// Posty adında komponentimi oluşturdum.
// Bu komponent çağrıldığında bir stack navigator exportlamak istediğim için ana komponent Musical'ımın
// içine PostStack stack navigator komponentimi yerleştirdim.
// Stack navigtor ımın içine screenler tanımladım.
export default class Posty extends React.Component{
render(){
return(
<PostStack />
);
}
}
// Yeni bir stack navigator oluşturdum ve adını PostNavigator koydum.
const PostNavigator = createStackNavigator({
Post: {screen: PostScreen},
NewPost: {screen: NewPostScreen}
});
// PostStack adlı containerımı yarattım ki Posty Component'inin içinde kullanabileyim.
const PostStack = createAppContainer(PostNavigator);
PostScreen.js
import React, { Component } from 'react';
import PostList from '../PostList'
import {TouchableOpacity} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome'
import { connect } from 'react-redux';
class PostScreen extends Component {
constructor(props){
super(props)
}
static navigationOptions = ({ navigation: { navigate } }) =>({
headerTitle: 'Posts',
headerRight:<TouchableOpacity onPress={() => navigate('NewPost')}>
<Icon style={{marginRight:15}} size={25} name='pencil' />
</TouchableOpacity>
})
render() {
return (
<PostList></PostList>
);
}
}
const mapStateToProps = state => {
return{
id: state.id
}
}
export default connect(mapStateToProps)(PostScreen);
NewPostScreen.js
import React, {Component} from 'react';
import {TextInput,View,Image,TouchableHighlight,StyleSheet,Text} from 'react-native';
import axios from 'axios';
import {connect} from 'react-redux';
class NewPostScreen extends Component {
constructor(props) {
super(props);
this.state = { text: 'What are you thinking?' };
}
onButtonClicked(){
console.log(this.state.text)
const {navigate} = this.props.navigation
axios.post("http://172.29.193.96:5000/newPost",
{
author_id: this.props.id,
content: this.state.text
}).then(
navigate('Post')
)
}
render() {
console.log("NewPostScreen id: ", this.props.id)
return (
<View>
<View style={{flexDirection:'row'}}>
<Image source={require('../../images/cat.png')}></Image>
<TextInput
style={{height: 100, width:350, textAlign:'auto', fontSize:20, marginTop:30, borderColor: 'gray', borderWidth: 1}}
onChangeText={(text) => this.setState({text})}
placeholder={this.state.text}
/>
</View>
<TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={this.onButtonClicked.bind(this)}>
<Text style={styles.loginText}>Ekle</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
buttonContainer: {
height:45,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop:20,
marginBottom:30,
marginLeft: 240,
width:150,
borderRadius:30,
},
textContainer: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 15,
width:150,
borderRadius:30
},
loginButton: {
backgroundColor: "#00b5ec",
},
loginText: {
color: 'white',
fontSize: 16
}
})
const mapStateToProps = state => {
return{
id: state.id
}
}
export default connect(mapStateToProps)(NewPostScreen);
So, in which method of a screen should I put my API call to be called again?
2
Answers
You have to use react lifecycle
You get the api result in datasource.