如何从子组件中使用seState?

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

How to use seState from child component?

问题

在这种情况下,如何从子组件中使用setDisabled

  1. const ChildButton = () => {
  2. const [disabled, setDisabled] = useState(false);
  3. return <div onClick={disabled ? null : console.log('do something')} />;
  4. }

ChildButton组件中导出:

  1. import ChildButton from './ChildButton';

Parent组件中,你可以这样使用setDisabled来控制子组件的状态并重新渲染子组件:

  1. const Parent = () => {
  2. const controllSetStateFun = () => {
  3. // 使用子组件的setDisabled函数,然后可以从Parent中重新渲染子组件
  4. };
  5. return (
  6. <div>
  7. <ChildButton />
  8. </div>
  9. );
  10. }
英文:

In the case, how to use setDisabled from child component ?

  1. const ChildButton = () =&gt; {
  2. const [disabled, setDisabled] = useState(false);
  3. return &lt;div onClick={disabled ? null : console.log(&#39;do something&#39;)} /&gt;
  4. }

import ChildButton from './ChildButton';

  1. const Parent = () =&gt; {
  2. const controllSetStateFun = () =&gt; {
  3. // use the child coomponent setDisabled then I can re-render child component from Parent
  4. };
  5. return (
  6. &lt;div&gt;
  7. &lt;ChildButton /&gt;
  8. &lt;/div&gt;
  9. );
  10. }

答案1

得分: 1

你需要将状态提升

  1. 在父组件中获取你的状态。
  2. 将设置状态的函数作为属性传递给子组件。
  3. 在子组件中调用setState函数。
  1. const Parent = () => {
  2. const [disabled, setDisabled] = useState(false);
  3. const controlSetStateFun = () => {
  4. // 使用子组件的setDisabled函数,以便可以从父组件重新渲染子组件
  5. };
  6. return (
  7. <div>
  8. <ChildButton disabled={disabled} setDisabled={setDisabled} />
  9. </div>
  10. );
  11. }
  12. const ChildButton = ({ disabled, setDisabled }) => {
  13. // 你可以从这里访问setDisable
  14. // ...
  15. return <div onClick={disabled ? null : console.log('做某事')} />;
  16. }
英文:

You need to lift the state up

  1. Take your state in the parent component.

  2. Pass the set state function
    as a prop to the child component.

  3. Call the setState function from
    the child component.

    const Parent = () => {
    const [disabled, setDisabled] = useState(false);

    const controllSetStateFun = () => {
    // use the child coomponent setDisabled then I can re-render child component from Parent
    };

    return (
    <div>
    <ChildButton disabled={disabled} setDisabled={setDisabled} />
    </div>
    );
    }

    const ChildButton = ({disabled,setDisabled}) => {
    //you can access setDisable from here
    ...
    return <div onClick={disabled ? null : console.log('do something')} />
    }

答案2

得分: 1

你可以通过传递props来实现这一点。

  1. const ChildButton = ({ disabled, setDisabled }) => {
  2. return <div onClick={disabled ? null : console.log('做一些事情')} />;
  3. }
  4. const Parent = () => {
  5. const [disabled, setDisabled] = useState(false);
  6. const controllSetStateFun = () => {
  7. // 使用子组件的setDisabled函数,然后我可以从父组件重新渲染子组件
  8. };
  9. return (
  10. <div>
  11. <ChildButton disabled={disabled} setDisabled={setDisabled} />
  12. </div>
  13. );
  14. }
英文:

you can do this by passing the props also

  1. const ChildButton = ({disabled, setDisabled}) =&gt; {
  2. return &lt;div onClick={disabled ? null : console.log(&#39;do something&#39;)} /&gt;
  3. }
  4. const Parent = () =&gt; {
  5. const [disabled, setDisabled] = useState(false);
  6. const controllSetStateFun = () =&gt; {
  7. // use the child coomponent setDisabled then I can re-render child component from Parent
  8. };
  9. return (
  10. &lt;div&gt;
  11. &lt;ChildButton disabled={disabled} setDisabled={setDisabled}/&gt;
  12. &lt;/div&gt;
  13. );
  14. }

答案3

得分: 1

请定义父组件中的状态,并从子组件中进行更新。

  1. const Parent = () => {
  2. const [disabled, setDisabled] = useState(false);
  3. const controlSetStateFun = () => {
  4. // 使用子组件的 setDisabled 函数,然后我可以从父组件重新渲染子组件
  5. };
  6. return (
  7. <div>
  8. <ChildButton visibility={disabled} visibilityfunction={setDisabled} />
  9. </div>
  10. );
  11. }
  12. const ChildButton = (props) => {
  13. return <div onClick={props.visibilityfunction} />;
  14. }
英文:

Please Define the state in Parent Component and update from the child.

  1. const Parent = () =&gt; {
  2. const [disabled, setDisabled] = useState(false)
  3. const controllSetStateFun = () =&gt; {
  4. // use the child coomponent setDisabled then I can re-render child component from Parent
  5. };
  6. return (
  7. &lt;div&gt;
  8. &lt;ChildButton visibility ={disbales} visibilityfunction ={setDisabled} /&gt;
  9. &lt;/div&gt;
  10. );
  11. }
  12. const ChildButton = (props) =&gt; {
  13. return &lt;div onClick={props.visibilityfunction} /&gt;
  14. }

huangapple
  • 本文由 发表于 2023年1月9日 16:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054742.html
匿名

发表评论

匿名网友

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

确定