Laravel and React not one of my strong points. You need to explicitly pass the 'setMyState' function down from the parent component to your 'ProductsMy' component -
Using a function in `setState` instead of an object[
^]
Remove the '{children}' prop from your States2 component since it will not be used.
Pass the 'setMyState' function to your 'ProductsMy' component as a prop -
In your States2 component -
import { useState } from 'react';
import Basket from './Basket';
export default function States2() {
const [myState, setMyState] = useState(0);
return (
<div className="App">
<header className="App-header">
<Basket myState={myState} />
</header>
<ProductsMy myState={myState} setMyState={setMyState} /> {}
</div>
);
}
In your 'ProductsMy' component, use the 'setMyState' prop directly in the 'handleToBasketClick' function -
import Button from 'react-bootstrap/Button';
export default function ProductsMy({ myState, setMyState }) {
const handleToBasketClick = () => {
setMyState(myState + 1);
};
const products3 = [
{ title: 'Product 1' },
{ title: 'Product 2' },
{ title: 'Product 3' },
{ title: 'Product 4' },
{ title: 'Product 5' },
];
return (
<div>
{products3.map((product, index) => (
<div className="flex items-center" key={index}>
<div>{product.title}</div>
<Button onClick={handleToBasketClick}>Add to Basket</Button>
</div>
))}
</div>
);
}
[EDIT/ADDITIONAL INFORMATION]
As the above returns a similar outcome for you when you uncomment
, you can use an alternative solution using 'React.cloneElement' -
React.cloneElement() Function[
^]
Remove the import of the 'ProductsMy' component from your States2 component, as it will be rendered through the '{children}' prop.
In your 'States2' component, use 'React.cloneElement' to pass the 'setMyState' function to your 'ProductsMy' component when rendering it through the '{children}' prop -
States2 component -
import React, { useState, useEffect } from 'react';
import Basket from './Basket';
export default function States2({ children }) {
const [myState, setMyState] = useState(0);
useEffect(() => {
const updateState = (newState) => {
setMyState(newState);
};
const clonedChildren = React.Children.map(children, (child) => {
return React.cloneElement(child, { myState, updateState });
});
return () => {};
}, [children]);
return (
<div className="App">
<header className="App-header">
<Basket myState={myState} />
</header>
{children}
</div>
);
}
ProductsMy -
import React from 'react';
import Button from 'react-bootstrap/Button';
export default function ProductsMy({ myState, updateState }) {
const handleToBasketClick = () => {
updateState(myState + 1);
};
const products3 = [
{ title: 'Product 1' },
{ title: 'Product 2' },
{ title: 'Product 3' },
{ title: 'Product 4' },
{ title: 'Product 5' },
];
return (
<div>
{products3.map((product, index) => (
<div className="flex items-center" key={index}>
<div>{product.title}</div>
<Button onClick={handleToBasketClick}>Add to Basket</Button>
</div>
))}
</div>
);
}
Maybe not exactly what you require but you can tweak this to work for you.