I know how to send data from child to parent component when a button is clicked, but the button should be in child component.
What I want is, how to send data from child to parent when a button is clicked, but the button is in parent component not in child component.
For instance, I have something like this:
Parent component
function ParentComponent() {
const handleChildData = (childData) => {
console.log(childData);
}
return (
<>
<ChildComponent onDataGet={handleChildData} />
<div>ParentComponent</div>
</>
)
}
Child component
function ChildComponent({onDataGet}) {
const data = {
firstName: "John",
lastName: "Smith",
}
// Based on something, execute this function:
onDataGet(data)
return (
<div>ChildComponent</div>
)
}
I have tried this approach:
defined a state, on each click the state increments by one, also passed this state to child, in there anytime the state gets changed, useEffect
in child component will happen and will execute the onDataGet
function. Like this:
Parent Component
function ParentComponent() {
const [isTrigerred, setIsTrigerred] = useState(1);
const handleChildData = (childData) => {
console.log(childData);
};
return (
<>
<ChildComponent onDataGet={handleChildData} gotChanged={isTrigerred} />
<div>ParentComponent</div>
<Button variant="contained" onClick={() => setIsTrigerred((prev) => prev + 1)}>
Click Me
</Button>
</>
);
}
Child Component
function ChildComponent({ onDataGet, gotChanged}) {
const data = {
firstName: "John",
lastName: "Smith",
};
useEffect(() => {
// Don't execute in first render
if (gotChanged !== 1) {
onDataGet(data);
}
}, [gotChanged]);
return <div>ChildComponent</div>;
}
But I’m looking for a better approach, if there is any.
Thanks.
2
Answers
One approach may be to pass a setter for the wanted function in your child component like this :
Parent Component :
Child Component :
The caching function of ref can be used to realize the method of parent calling child components.