Parent Component
The parent component contains input which on change sets the state "local" and a button which on click takes this "local" state's value and sets it to "sendLocal"
Functions
changehandler : triggers on input type change.
sendLocalValue : takes "local" value puts it into "sendLocal" variable triggers on button click.
sendValue : this memoized function with dependeny "sendLocal" is passed on as a prop in child component triggers once the child is rendered.
import React, { useState, useCallback } from "react"; import ChildComponent from "./ChildComponent"; function ParentComponent() { const [local, setLocal] = useState(); const [sendLocal, setsendLocal] = useState(); const changehandler = (e) => { console.log("parent"); setLocal(e.target.value); }; const sendLocalValue = () => { setsendLocal(local); }; const sendValue = useCallback(() => { return sendLocal; }, [sendLocal]); return ( <> <input type="text" onChange={changehandler} defaultValue={local} /> <button onClick={sendLocalValue}>send</button> <ChildComponent getValue={sendValue} /> </> ); } export default ParentComponent;
Child Component
getValue prop calls the memoized "sendValue" function of parent which returns the value of sendLocal.
Problem
Everything works fine,the child component renders only when the "sendLocal" value changes on button click but if i remove React.memo() in child both the component render on input type change even with useCallback() used, why?
import React, { useEffect, useState } from "react";
function ChildComponent({ getValue }) {
console.log("child");
return <div>child's {getValue()}</div>;
}
export default React.memo(ChildComponent);