传值的几种方式
StackNavigator
从A跳转到B
// A
this.props.navigation.navigate('B',{name: 'name', value: 'value'})
// B
let params = this.props.navigation.state.params
let name = params.name
let value = params. value
父组件A给子组件B传值
// A
<B
name=this.state.name
/>
// B
<Input name=this.props.name />
A->B->C->D, D 完成了某个操作,业务逻辑上要刷新A界面
// A
import {
DeviceEventEmitter,
} from 'react-native';
componentDidMount() {
// 注册监听,接收通知
this.readEventListener = DeviceEventEmitter.addListener('read', (somePassedValue)=>{
// do your things
})
}
componentWillUnmount() {
this.readEventListener && this.readEventListener.remove()
}
// D
import {
DeviceEventEmitter,
} from 'react-native';
onReadBtnPress() {
// 发送通知
DeviceEventEmitter.emit('read', true)
}