如何向react-native-router-flux的Actions.replace传递props?
解决react-native-router-flux v4.0.0-beta.28中Actions.replace传递props的问题
针对你使用的react-native-router-flux v4.0.0-beta.28和react-native v0.52.2版本,我来帮你梳理下Actions.replace传递props的正确用法——你之前的写法在参数结构上有小问题:
正确写法1:直接调用Actions.replace
Actions.replace的第一个参数是目标路由的key,第二个参数就是要传递的props对象,不需要把key嵌套到对象里:
Actions.replace(tabKey, { tabPage: tabPage });
正确写法2:通过Actions[key]指定替换类型
用这种方式时,要把传递的props直接放在配置对象中,不需要额外包裹props属性:
import { ActionConst } from 'react-native-router-flux'; Actions[tabKey]({ type: ActionConst.REPLACE, tabPage: tabPage });
目标页面接收props的方式
在对应tabKey的Scene组件里,直接通过this.props.tabPage就能拿到传递的值,比如:
class TargetTabPage extends React.Component { componentDidMount() { console.log('收到的页面参数:', this.props.tabPage); } // ...组件其他逻辑 }
额外注意点
- 务必保证
tabKey和你在Router中定义的Scene的key完全一致,否则无法匹配到目标路由 - 这个beta版本的API和后续正式版有差异,建议严格对应当前版本的写法,不要混用其他版本的示例
内容的提问来源于stack exchange,提问作者muppet11
相关产品推荐
相关产品推荐

