java获取监听程序的状态_React Native和redux在侦听器中获取当前状态

我正在尝试使用react-native和redux访问侦听器或辅助类中的当前状态 .

我们使用react-native-tcp从tcp套接字获取数据(使用nodejs网络库) . 我试图缩短代码作为更好理解的例子 .

目前的情况:

on('data')上的套接字侦听器在componentDidMount中注册 . 如https://reactjs.org/docs/react-component.html#componentdidmount中所述,这应该是一个好点"to load data from a remote endpoint" . 只有我们注册一个监听器,并且不会获取一次数据 .

配置由configurationReducer存储在状态中,configurationReducer将配置加载到componentDidMount上的存储中 . 我们看到这可以通过redux-logger工作 . reducer的初始状态是一个空数组 .

问题:

套接字侦听器中的帮助器方法只能访问配置的初始状态,并且不会更新传递的props .

问题:

componentDidMount()是注册套接字侦听器的最佳位置吗?

如何传递配置道具或使当前状态可用于侦听器的回调函数?

doSomething(data)帮助器方法中有一些逻辑用于将数据解析为人类可读对象 . 目前,这是由ES6类(辅助类)完成的 . 这是正确的地方还是应该在减速器中进行逻辑?

然后我通过使用“store.getState() . configuration”传递我的配置状态 . 如果有人仍然对我上面的问题提出建议,我会很高兴听到他们的意见 .

class MyApp extends Component {

componentDidMount() {

// ... create socket, connect to server

// add configuration to state by reducer = [ configuration: [ 'config1': 1, 'config2': 2 ] ]

// register listener to handle data, https://nodejs.org/api/net.html

socket.on('data', (data) => {

doSomething(data);

});

const { configuration } = this.props; // I think here is the problem, because componentDidMount does only have access to the initial state.

function doSomething(data) {

console.log(configuration);

// first problem: configuration is always initial state = [] and will not be updated.

}

}

}

const mapStateToProps = state => {

return {

configuration: state.configuration,

};

};

export default connect(mapStateToProps)(MyApp);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值