使用React + Redux时,父道具会从状态覆盖道具,还是状态道具会覆盖父组件的道具(如果道具名称发生冲突,这是吗?
interface OwnProps {
//The type for the props provided by the parent component
}
function mapDispatch(dispatch: AppDispatch<any>) {
return { dispatch };
}
function mapState(state: RootState) {
return {
s: {
// props from state are namespaced onto s
}
};
}
type StateProps = ReturnType<typeof mapState>;
type DispatchProps = ReturnType<typeof mapDispatch>;
type AllProps = StateProps & DispatchProps & OwnProps;
const ResetPassword: React.FC<AllProps> = (p) => {
///
}
const Component = ({ testProp }) => (
<div>testProp: {testProp}</div>
);
const mStP = () => ({
testProp: 'testProp from mapStateToProps',
});
const ConnectedComponent = connect(mStP)(Component);
然后在您的App组件中的某个位置执行此操作:
<ConnectedComponent testProp="testProp from parent component" />
如您所见,mapStateToProps
决定testProp
的值。如果要保留父组件传递的值,只需不要将testProp
放在mapStateToProps
返回的对象中。
[如果您不使用ownProps
中的第二个mapStateToProps
参数,则传递给ConnectedComponent
的所有道具仍会在那里-Redux不会干扰默认的React行为。