I have made a simple component mapper, it is working, but I don't know how to pass the props onto the To Be Returned Component. Here's my current codes:
In my sample, I would like to render and pass props on to the <SampleComponent/> by using mapComponent('employee__create', component.props)
mapComponent.jsx
import SampleComponent from './SampleComponent';
import SampleComponentTwo from './SampleComponentTwo';
const component_list = {
employee: {
create: <SampleComponent />,
update: <SampleComponentTwo />,
},
};
const mapComponent = (path, props) => {
let to_render = component_list;
path.split('__').forEach((path) => {
to_render = to_render[path];
});
return to_render;
};
export default mapComponent;
PersistentWindow.jsx
import mapComponent from '../../samples/mapComponent';
const PersistentWindow = (props) => {
const component_path = props.component.path //equals to 'employee__create';
return (
<div {...props.window}>
{mapComponent(component_path, props.component.props)}
</div>
);
};
export default PersistentWindow;
I can pass and use the props when component_list is declared inside mapComponent as shown in the sample below. But I think that will also unwantedly pass the props to all other Components on the list.
const mapComponent = (path, props) => {
const component_list = {
employee: {
create: <SampleComponent {...props}/>,
update: <SampleComponentTwo {...props}/>,
},
};
let to_render = component_list;
path.split('__').forEach((path) => {
to_render = to_render[path];
});
return to_render;
};
export default mapComponent;
How do I do this? or any better implementations?
P.S. if you can also think of better title for this post will be much appreciated, so it may reach right audiences.