【发布时间】:2021-01-28 08:04:42
【问题描述】:
我想通过 redux 传递一个组件并在另一个组件中显示它。 我正在做这样的事情:
ComponentToDispatch.js
const ComponentToDispatch = (props) => {
return (<div>Propagate me {props.anthem}</div> {/* props.anthem = undefined, unlike what I expect.*/}
);
};
export {ComponentToDispatch};
在以下组件中,我有一个按钮,用于调度之前定义的按钮。
BruitEvent.js
// code above
`import {ComponentToDispatch} from "./ComponentToDispatch";
import {showComponentAction} from "./actions";
const BruitEvent =()=>{
const dispatch = useDispatch();
return (<button onClick={(evt)=>dispatch(showComponentAction(ComponentToDispatch)}>
Click to dispatch</button>);
};`
触发此事件的动作是: 动作.js
`
export function ShowComponentAction(Component) {
return {
type: SHOW_ACTION,
payload: {
component: <Component />,
},
};
}`
最后,我可以显示传播的组件了:
const DispayComponent = () =>{
const { component} = useSelector((state) => {
if (state.testDisplay) {
return {
component: state.testDisplay.component,
};
}
return { component: null };
});
useInjectReducer({ key: "testDisplay", reducer });
return (<div>{component}</div>);
}
export {DisplayComponent};
到目前为止一切顺利,感谢 David Hellsing for his insight,我可以显示驻留在 `ComponentToDispatch' 中的所有静态事物,但它无法处理 props。
问题:如何在 dispatch 组件的同时传递 props?
【问题讨论】:
标签: reactjs redux react-redux react-boilerplate