问题描述
我有一个在 React Native Navigator 组件中创建的 react-redux 容器组件.我希望能够将导航器作为道具传递给此容器组件,以便在其展示组件内按下按钮后,它可以将对象推送到导航器堆栈上.
I have a react-redux container component that is created within a React Native Navigator component. I want to be able to pass the navigator as a prop to this container component so that after a button is pressed inside its presentational component, it can push an object onto the navigator stack.
我想在不需要手写 react-redux 容器组件给我的所有样板代码的情况下做到这一点(也不要错过 react-redux 在这里给我的所有优化).
I want to do this without needing to hand write all the boilerplate code that the react-redux container component gives me (and also not miss out on all the optimisations that react-redux would give me here too).
示例容器组件代码:
const mapStateToProps = (state) => {
return {
prop1: state.prop1,
prop2: state.prop2
}
}
const mapDispatchToProps = (dispatch) => {
return {
onSearchPressed: (e) => {
dispatch(submitSearch(navigator)) // This is where I want to use the injected navigator
}
}
}
const SearchViewContainer = connect(
mapStateToProps,
mapDispatchToProps
)(SearchView)
export default SearchViewContainer
而且我希望能够在我的导航器 renderScene
函数中调用这样的组件:
And I'd want to be able to call the component like this from within my navigator renderScene
function:
<SearchViewContainer navigator={navigator}/>
在上面的容器代码中,我需要能够从 mapDispatchToProps
函数中访问这个传递的 prop.
In the container code above, I'd need to be able to access this passed prop from within the mapDispatchToProps
function.
我不喜欢将导航器存储在 redux 状态对象上,也不想将 prop 传递给展示组件.
I don't fancy storing the navigator on the redux state object and don't want to pass the prop down to the presentational component.
有没有办法将 prop 传递给这个容器组件?或者,有没有我忽略的替代方法?
Is there a way I can pass in a prop to this container component? Alternatively, are there any alternative approaches that I'm overlooking?
谢谢.
推荐答案
mapStateToProps
和 mapDispatchToProps
都将 ownProps
作为第二个参数.
mapStateToProps
and mapDispatchToProps
both take ownProps
as the second argument.
[mapStateToProps(state, [ownProps]): stateProps] (Function):
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
对于 参考
这篇关于将 props 传递给 react-redux 容器组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!