Complete the code to pass a prop named message from the parent to the child component.
function Parent() {
const greeting = "Hello!";
return <Child [1]={greeting} />;
}
function Child(props) {
return <p>{props.message}</p>;
}The parent passes a prop named message to the child. The child accesses it via props.message.
Complete the code to update the parent state when the child button is clicked.
function Parent() {
const [count, setCount] = React.useState(0);
function handleClick() {
setCount(count + 1);
}
return <Child [1]={handleClick} />;
}
function Child(props) {
return <button onClick={props.onClick}>Click me</button>;
}The parent passes a function prop named onClick. The child uses it as the button's click handler.
Fix the error in the child component to correctly receive and use the prop onAction.
function Child({ [1] }) {
return <button onClick={onAction}>Do Action</button>;
}onAction directly.Using destructuring, the child must name the prop exactly as onAction to use it directly.
Fill both blanks to pass a state value and its setter function from parent to child.
function Parent() {
const [name, setName] = React.useState('Alice');
return <Child [1]={name} [2]={setName} />;
}
function Child(props) {
return <input value={props.name} onChange={e => props.setName(e.target.value)} />;
}The parent passes the state name and the setter setName as props to the child.
Fill all three blanks to create a controlled input where the parent manages the state and the child updates it.
function Parent() {
const [email, setEmail] = React.useState('');
return <Child [1]={email} [2]={e => setEmail(e.target.value)} [3]="Enter email" />;
}
function Child({ [1], [2], [3] }) {
return <input value={email} onChange={onChange} placeholder={placeholder} />;
}The parent passes email, an onChange handler, and a placeholder string. The child destructures these props and uses them in the input.