I am new to React. What I am trying to achieve is to render a new component when a button is clicked in the parent component and pass some data from parent to child. I am able to navigate from parent to child but I am unable to pass data. Below is the simplified version of my code. My parent component - Posts.js
const Posts = (props) => {
const { posts, loading } = props;
var nameArray = posts.map(function (el) {
return el.kiosk_name;
});
console.log(nameArray);
if (loading) {
return <h2>Loading All Kiosks....</h2>;
}
return (
<div>
<Link to={`/kiosks/addkiosk`}>
<button className="AddBtn" onClick={nameArray}>ADD</button>
</Link>
)}
Child component - AddKiosk.js
export const AddKiosk = (props) => {
const { nameArray } = props;
console.log(nameArray);
}
I am trying to pass the nameArray from onClick but the console log of AddKiosk shows - Undefined. Please let me know where am I going wrong. Sorry if my question is very trivial. Thanks in advance.