I have successfully passed props to another screen, but how do I use the props globally on the second screen?
I need to be able to use the props passed from screen 1 in all functions including render()
, as I am making a fetch call with data from the props and using the same props data I the render()
call.
For example, I need to do this:
constructor(props) {
super(props);
this.state = {
data: [],
rowData: this.props,
}
}
getData() {
this.setState({
rowData: this.props
});
return fetch('https://mywebsite.com/'+this.state.rowData.something+'/myotherdata')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
data: responseJson.data
});
})
.catch((error) => {
console.error(error);
});
}
componentDidMount() {
this.getData();
}
render() {
return (
<ScrollView>
<Image styleName="large-banner" source={{ uri: rowData.image &&
rowData.image ? rowData.image : undefined }}>
<Tile>
<Title>{rowData.name}</Title>
<Subtitle>{rowData.createdAt.datetime}</Subtitle>
</Tile>
</Image>
<Row>
<Text>Company: {rowData.company}</Text>
</Row>
<Divider styleName="line" />
<Row>
<Icon name="laptop" />
<View styleName="vertical">
<Subtitle>Visit webpage</Subtitle>
<Text>{rowData.url}</Text>
</View>
<Icon name="right-arrow" />
</Row>
<Divider styleName="line" />
<View style={{paddingTop: 20}}>
<Progress.Bar progress={this.state.data.progress * .1} width={200} />
</View>
<Divider styleName="line" />
</ScrollView>
);
}
Obviously, I am not doing something right, but I can't figure it out. What am I doing wrong?
This question explains more about my screens and fetch
calls.
EDIT: I made changes based on the answer below and update my code. The screen loads and then gives this error:
undefined is not an object (evaluating 'this.state.data.progress)
for this line:
<Progress.Bar progress={this.state.data.progress * .1} width={200} />