简体   繁体   English

React Native和Redux道具正确的设计模式

[英]React Native & Redux props correct design pattern

I have a react native project and I am using redux actions and reducers to handle my store. 我有一个React Native项目,正在使用Redux动作和reducer处理我的商店。
I have a smart connected component and I call my actions through dispatch in componentWillMount() and then I have mapStateToProps and I map the data I get from my store to props. 我有一个智能连接的组件,我通过componentWillMount()调度调用了操作,然后有了mapStateToProps并将从商店中获取的数据映射到道具。
Problem is the only way to check if the props are changed, is by putting it inside render function. 问题是检查道具是否更改的唯一方法是将其放在render函数中。 In my case I want to navigate to another page if I get props filled with some data. 就我而言,如果我的道具填充了一些数据,我想导航到另一个页面。 It all works fine but I get warning from React that its anti pattern. 一切正常,但是我从React那里得到警告,它的反模式。 Is there any other way of doing this? 还有其他方法吗? Am I doing it right? 我做对了吗?

You should implement the method componentWillReceiveProps instead of comparing the values in the render 您应该实现componentWillReceiveProps方法,而不是比较render的值

Another (preferable) approach would be the action responsible to update the values, do the navigation (or dispatch another actions that would do the navigation stuff). 另一种(较好的)方法是负责更新值,进行导航(或调度将执行导航任务的其他动作)的动作。 Check redux-thunk 检查redux-thunk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM