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.
.splitcreates an array of strings that you then.forEach, you overwriteto_rendertwice, and return the last set value... it's unclear if you are mapping anything, or what you want the expected result to be. Can you clarify a bit more what you are expecting the rendered result to be? Are you basically just string splitting the path to get to the nested component you actually want to render?<SampleComponent />along with its props into the<PersistentWindow/>component by usingmapComponent('employee__create', component.props). the.split + forEachportion there ultimately ends up selecting<SampleComponent/>which is to be returned on the<PersistentWindow/>component