, , . React Native, - .
React Navigation API docs ! ! .
, StackNavigator. API. StackNavigator pop.
handleSubmit = () => {
const { value, otherValue } = this.state
addThingToDatabase({ value, otherValue })
.then(() => this.props.navigation.pop())
}
, "" StackNavigator. "pop". ComponentDidMount.
componentDidMount() {
const { index } = this.props.navigation.state.params
getAllThingsFromDatabase({ index })
.then(({ arrayOfThings }) => this.setState({
index,
arrayOfThings
}))
}
But the Component was not updated with the new thing until addListener! Now I have almost the same code, except for it in the constructor. I decided that I only need to run it once, and I also need to store it.
constructor(props, context) {
super(props, context)
this.state = {
index: null,
arrayOfThings: []
}
this.willFocusSubscription = this.props.navigation.addListener(
'willFocus',
(payload) => {
const { index } = payload.state.params
getAllThingsFromDatabase({ index })
.then(({ arrayOfThings }) => this.setState({
index,
arrayOfThings
}))
}
)
}
Please note that the docs also mention unsubscribing from the event listener using the function .remove(). I put it in ComponentWillUnmount().
componentWillUnmount() {
this.willFocusSubscription.remove()
}
There are four different events for a subscription. I went with willFocus, thinking that it will be updated before the screen appears.
source
share