I want to pass a component and display it inside another one through redux. I am doing something like this:
ComponentToDispatch.js
const ComponentToDispatch = (props) => {
return (<div>Propagate me {props.anthem}</div> {/* props.anthem = undefined, unlike what I expect.*/}
);
};
export {ComponentToDispatch};
In the following component, I have a button that dispatches the formerly defined one.
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>);
};`
The action that triggers this event is: actions.js
`
export function ShowComponentAction(Component) {
return {
type: SHOW_ACTION,
payload: {
component: <Component />,
},
};
}`
Finally, I can display the propagated 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};
So far so good, thanks to David Hellsing for his insight, I can display every static thing resides in `ComponentToDispatch', but it fails to handle props.