Here I'm trying to use the el data inside the Composition Component.I'm trying like this :
const Game = ["Cricket", "Football", "Badminton"];
const Test = (props) => {
return (
<div>
<ul>
{Game.map((el) => (
<li key={Math.random()}>
<span>{el}</span>
{props.children}
</li>
))}
</ul>
</div>
);
};
const Composition = () => {
return (
<Test>
<div>{el}</div>
</Test>
);
};
export default Composition
But it's give me an error. I can't understand how to do this. Is there any way to get the el data inside Composition Component from Test Component ? here is my app.js file :
import { Fragment } from "react";
import Composition from "./Components/Header/Composition";
export default function App() {
return (
<Fragment>
<Composition></Composition>
</Fragment>
);
}
Working example:
const Game = ["Cricket", "Football", "Badminton"];
const Test = (props) => {
return (
<div>
<ul>
{Game.map((el) => (
<li key={Math.random()}>
<span>{el}</span>
{props.children({ el })}
</li>
))}
</ul>
</div>
);
};
const MyDiv = ({ el }) => {
return <div>{el}</div>
}
const Composition = () => {
return (
<Test>
{({ el }) => <MyDiv el={el} />}
</Test>
);
};
ReactDOM.render(
<Composition />,
document.querySelector('body')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>