将 props 传递给 react-redux 容器组件
Passing props to react-redux container component
我有一个在 React Native Navigator 组件中创建的 react-redux 容器组件。我希望能够将导航器作为道具传递给此容器组件,以便在其展示组件内按下按钮后,它可以将对象推送到导航器堆栈上。
我想这样做而不需要手写 react-redux 容器组件给我的所有样板代码(也不要错过 react-redux 在这里也会给我的所有优化)。
示例容器组件代码:
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
函数中这样调用组件:
<SearchViewContainer navigator={navigator}/>
在上面的容器代码中,我需要能够从 mapDispatchToProps
函数中访问这个传递的 prop。
我不想将导航器存储在 redux 状态对象上,也不想将 prop 传递给展示组件。
有什么方法可以将 prop 传递给这个容器组件吗?或者,有没有我忽略的替代方法?
谢谢。
您可以将第二个参数传递给 mapStateToProps(state, ownProps)
,这将使您能够访问传递到 mapStateToProps
中的组件的道具
mapStateToProps
和 mapDispatchToProps
都将 ownProps
作为第二个参数。
[mapStateToProps(state, [ownProps]): stateProps] (Function):
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
使用装饰器 (@)
如果你正在使用装饰器,下面的代码给出了一个例子,你想为你的 redux 连接使用装饰器。
@connect(
(state, ownProps) => {
return {
Foo: ownProps.Foo,
}
}
)
export default class Bar extends React.Component {
如果您现在检查 this.props.Foo
,您将看到从使用 Bar
组件的地方添加的道具。
<Bar Foo={'Baz'} />
在这种情况下 this.props.Foo
将是字符串 'Baz'
希望这能澄清一些事情。
使用打字稿执行此操作时会遇到一些问题,所以这里有一个示例。
一个问题是当你只使用 dispatchToProps(而不映射任何状态道具)时,重要的是不要省略状态参数,(它可以用下划线前缀命名)。
另一个问题是 ownProps 参数必须使用仅包含传递的 props 的接口来键入 - 这可以通过将 props 接口拆分为两个接口来实现,例如
interface MyComponentOwnProps {
value: number;
}
interface MyComponentConnectedProps {
someAction: (x: number) => void;
}
export class MyComponent extends React.Component<
MyComponentOwnProps & MyComponentConnectedProps
> {
....// component logic
}
const mapStateToProps = (
_state: AppState,
ownProps: MyComponentOwnProps,
) => ({
value: ownProps.value,
});
const mapDispatchToProps = {
someAction,
};
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
可以通过传递单个参数来声明组件:
<MyComponent value={event} />
我有一个在 React Native Navigator 组件中创建的 react-redux 容器组件。我希望能够将导航器作为道具传递给此容器组件,以便在其展示组件内按下按钮后,它可以将对象推送到导航器堆栈上。
我想这样做而不需要手写 react-redux 容器组件给我的所有样板代码(也不要错过 react-redux 在这里也会给我的所有优化)。
示例容器组件代码:
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
函数中这样调用组件:
<SearchViewContainer navigator={navigator}/>
在上面的容器代码中,我需要能够从 mapDispatchToProps
函数中访问这个传递的 prop。
我不想将导航器存储在 redux 状态对象上,也不想将 prop 传递给展示组件。
有什么方法可以将 prop 传递给这个容器组件吗?或者,有没有我忽略的替代方法?
谢谢。
您可以将第二个参数传递给 mapStateToProps(state, ownProps)
,这将使您能够访问传递到 mapStateToProps
mapStateToProps
和 mapDispatchToProps
都将 ownProps
作为第二个参数。
[mapStateToProps(state, [ownProps]): stateProps] (Function):
[mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
使用装饰器 (@)
如果你正在使用装饰器,下面的代码给出了一个例子,你想为你的 redux 连接使用装饰器。
@connect(
(state, ownProps) => {
return {
Foo: ownProps.Foo,
}
}
)
export default class Bar extends React.Component {
如果您现在检查 this.props.Foo
,您将看到从使用 Bar
组件的地方添加的道具。
<Bar Foo={'Baz'} />
在这种情况下 this.props.Foo
将是字符串 'Baz'
希望这能澄清一些事情。
使用打字稿执行此操作时会遇到一些问题,所以这里有一个示例。
一个问题是当你只使用 dispatchToProps(而不映射任何状态道具)时,重要的是不要省略状态参数,(它可以用下划线前缀命名)。
另一个问题是 ownProps 参数必须使用仅包含传递的 props 的接口来键入 - 这可以通过将 props 接口拆分为两个接口来实现,例如
interface MyComponentOwnProps {
value: number;
}
interface MyComponentConnectedProps {
someAction: (x: number) => void;
}
export class MyComponent extends React.Component<
MyComponentOwnProps & MyComponentConnectedProps
> {
....// component logic
}
const mapStateToProps = (
_state: AppState,
ownProps: MyComponentOwnProps,
) => ({
value: ownProps.value,
});
const mapDispatchToProps = {
someAction,
};
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
可以通过传递单个参数来声明组件:
<MyComponent value={event} />