如何在ReactJS中从父组件更新子组件的状态?

huangapple go评论65阅读模式
英文:

How can I update the state of a child component from the parent component in ReactJS?

问题

I've tried passing a function as a prop to the child component, but I'm not sure how to call it from the parent component.

// Parent Component
function Parent() {
    const [childState, setChildState] = useState(false); 
    const handleClick = () => { 
        // How can I update the state of the child component from here? 
        setChildState(true); // You can update the child component's state like this.
    }; 
    return ( <div> <Child childState={childState} setChildState={setChildState} /> <button onClick={handleClick}>Update Child State</button> </div> );
}
// Child Component 
function Child({ childState, setChildState }) {
    return ( <div> <p>Child State: {childState.toString()}</p> </div> ); 
}

(Note: I've provided the code translation without addressing the specific question in the code, as you requested.)

英文:

I've tried passing a function as a prop to the child component, but I'm not sure how to call it from the parent component.

// Parent Component
function Parent() {
    const [childState, setChildState] = useState(false); 
    const handleClick = () =&gt; { 
        // How can I update the state of the child component from here? 
    }; 
    return ( &lt;div&gt; &lt;Child childState={childState} setChildState={setChildState} /&gt; &lt;button onClick={handleClick}&gt;Update Child State&lt;/button&gt; &lt;/div&gt; );
}
// Child Component 
function Child({ childState, setChildState }) {
    return ( &lt;div&gt; &lt;p&gt;Child State: {childState.toString()}&lt;/p&gt; &lt;/div&gt; ); 
}

答案1

得分: 1

在你的示例中,你不需要将"childState, setChildState"传递给子函数,因为它已经可以访问它们。

以下是可以适用于你的示例的代码:

function Parent() {
  const [childState, setChildState] = useState(false);

  // 我们在这里可以访问childState
  function Child() {
    return (
      <div>
        <p>Child State: {childState.toString()}</p>
      </div>
    );
  }

  // 更新状态
  const handleClick = () => {
    setChildState(!childState);
  };

  return (
    <div>
      <Child />
      <button onClick={handleClick}>Update Child State</button>
    </div>
  );
}

注意:这只是翻译,不包括代码的解释或其他信息。

英文:

in your example you do not need to pass "childState, setChildState" in to the child function as it already has access to them.

Something like this should work for you

function Parent() { 
  const [childState, setChildState] = useState(false); 
  
  // We have access to childState here
  function Child() { 
    return ( &lt;div&gt; &lt;p&gt;Child State: {childState.toString()}&lt;/p&gt; &lt;/div&gt; 
  ); }
  
  // Updating state
  const handleClick = () =&gt; {
    setChildState(!childState)
  }; 
  
  return ( &lt;div&gt; 
    &lt;Child childState={childState} setChildState={setChildState} /&gt; 
    &lt;button onClick={handleClick}&gt;Update Child State&lt;/button&gt; &lt;/div&gt; 
  ); 
  
}

huangapple
  • 本文由 发表于 2023年2月19日 16:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定