I'd like to change the props using react hooks, and I found the way passing setState function as props to the child.
Container.tsx
const Container: React.FC = () => {
const [num, setNum] = useState(0);
return <Counter num={num} setNum={setNum} />;
};
Counter.tsx
interface CounterProps {
num: number;
setNum: React.Dispatch<React.SetStateAction<number>>;
}
const Counter: React.FC<CounterProps> = ({ num, setNum }) => {
const handleClick = () => {
setNum(num + 1);
};
return (
// jsx codes...
);
};
It works well, but I have to add two props to the child component per one state of the parent. Is there a more efficient way for this problem?