The scenario is like I want to create a component which can accept component as an argument or props.
So created one functional component named as scenarioComponent.js with props
function ScenarioComponent(props) {
return <div> <h1>{props.Title}</h1>
<div className='row'>
{props.HTMLCode}//this will be the another component HTML tag
</div>
<div className='row'>
{props.REACTCode}//this will be the another component REACT code
</div>
</div>;
}
Once this component is ready we can call this to pass the necessary props:
below code in SampleComponet.js where i want to use scenario component.
function SampleComponet(props) {
import ScenarioComponent from './components/ScenarioComponent';
return ( <div>
<ScenarioComponent Title="My New component" HTMLCode={<div>My Heading<div>} REACTCode={import ScenarioComponent from '.components/ScenarioComponent';}>
</div>
);
}
I have tried above line of code and referred the text .
Able to achieve partially but want to make it more flexible like we should pass complex component.So any help will appreciated.
4
Answers
As I commented before,
You are almost there. The problem is you can’t put an
import
inside a prop. Instead, just do theimport
at the top of your file:Then pass the component as a prop:
On top of @Jyoung answer, if you still want to pass the elements as props, you can do like the below.
Another one :
Instead of passing components as props you wrap them with your
ScenarioComponent
component so you recieve them asprops.children
: