Listing 11-2: Setter functions are bound to their creator component
count value: 0
import {useState} from 'react';
function ButtonContainer(){
const [count,setCount] = useState(0);
return (
<>
<MyButton count = {count} setCount = {setCount} /><br />
count value: {count}
</>
);
}
function MyButton(props){
return (
<button onClick = {()=>props.setCount(props.count+1)}>
Add 1 to the Count
</button>
);
}
export {ButtonContainer,MyButton};
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.
