当前位置: 首页 > 知识库问答 >
问题:

通过父级更改同级状态

董高朗
2023-03-14

我的反应结构是

- App
   |--SelectStudy
   |--ParticipantsTable

SelectStudy中有一个按钮,单击该按钮可通过App家长向其兄弟姐妹ParticipantsTable发送消息。第一个孩子-

应用程序

class App extends Component { 

  myCallback(dataFromChild) {
      // This callback receives changes from SelectStudy Child Component's button click
      // THIS WORKS
      alert('SelectStudy Component sent value to Parent (App): ' + dataFromChild.label + " -> " + dataFromChild.value);
      // QUESTION: How to Update State of ParticipantsTable (SelectStudy's Sibling) next?
      // ........................................................
  }

  render() {
      return (
          <div className="App">
             <SelectStudy callbackFromParent={this.myCallback}></SelectStudy>
             <ParticipantsTable></ParticipantsTable>
          </div>
      );
}

选择研究

class SelectStudy extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            error: null,
            isLoaded: false,
            items: [],
            selectedStudy: null, 
            isButtonLoading: false
        };
        this.handleButtonClick = this.handleButtonClick.bind(this);
    }

    render() {
        const { error, isLoaded, items, itemsForReactSelect, selectedStudy, isButtonLoading } = this.state;
        return <Button onClick={this.handleButtonClick}>Search</Button>;
    }

    handleButtonClick = () => {
        this.props.callbackFromParent(this.state.selectedStudy);
    }

}

参与者表-这需要接收某个变量,例如在其状态中的研究

class ParticipantsTable extends React.Component {
    constructor(props) {
        //alert('Constructor');
        super(props);
        // Initial Definition of this component's state
        this.state = {
            study: null,
            items: [], 
            error: null
        };
    }

    // THIS METHOD IS AVAILABLE, BUT HOW TO CALL IT FROM App's myCallback(dataFromChild)?
    setStudy = (selectedStudy) => {
        this.setState({study: selectedStudy});
    }    

    render() {
        return ( <div>{this.state.study}</div> );
    }
}

共有2个答案

东郭思远
2023-03-14

我认为你需要理解的是stateprops之间的区别。

状态是组件的内部状态,而道具则从父级传递给子级

这里有一个深入的答案

因此,您希望在父级中设置一个状态,您可以将其作为道具传递给子级

1在父级中设置状态

 this.state = {
  value: null
 }

 myCallback(dataFromChild) {
    this.setState({value: dataFromChild.value})

  }

把它作为道具传给孩子们

class ParticipantsTable extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            study: props.study,
            items: [], 
            error: null
        };
    }

另外,尽管与您的问题无关,但如果您正在学习React,我建议您放弃基于类的组件,转而使用挂钩和功能组件,因为它们最近已经得到了更广泛的使用和流行。

郭永安
2023-03-14

状态应该明确地存在于App级别,而不是子级别。国家需要生活在需要获得它的最低公分母之上。因此,如果SseltResearch参与者表都需要访问相同位的状态数据,那么它必须生活在它们最接近的共同祖先(或以上)中。

这是React的核心概念,称为“提升状态”,在React官方文档中有自己的页面。

在你的情况下,它看起来像这样。注意状态如何只存在于一个地方,位于

import React from 'react';

class App extends React.Component {
  // State lives here at the closest common ancestor of children that need it
  state = {
    error: null,
    isLoaded: false,
    items: [],
    selectedStudy: null,
    isButtonLoading: false
  };

  myCallback = (dataFromChild) => {
    this.setState(dataFromChild);
  };

  render() {
    return (
      <div className="App">
        {/* State is passed into child components here, as props */}
        <SelectStudy data={this.state} callbackFromParent={this.myCallback}></SelectStudy>
        <ParticipantsTable study={this.state.selectedStudy} />
      </div>
    );
  }
}

class SelectStudy extends React.Component {
  handleButtonClick = () => {
    // Here we execute a callback, provided by <App />, to update state one level up
    this.props.callbackFromParent({ ...this.props.selectedStudy, isButtonLoading: true });
  };

  render() {
    const { error, isLoaded, items, itemsForReactSelect, selectedStudy, isButtonLoading } = this.props.data;

    return <Button onClick={this.handleButtonClick}>Search</Button>;
  }
}

// This component doesn't need to track any internal state - it only renders what is given via props
class ParticipantsTable extends React.Component {
  render() {
    return <div>{this.props.study}</div>;
  }
}

 类似资料:
  • 我有一个父状态,它有一个“主题”状态。 尝试将子元素的样式内联到 但问题是,当父组件状态更改时,子组件不会更新。当按下切换按钮在亮/暗之间改变状态时,我控制台记录父状态。父状态更新良好,但内联样式不变。指示子组件在加载页时被锁定为当前的任何状态。 这有什么原因吗? 我是新反应,所以很抱歉,如果这是一个愚蠢的问题! 家长 儿童

  • 我正在尝试制作一个很好的ApiWrapper组件来填充各种子组件中的数据。从我读到的所有内容来看,这应该是可行的:https://jsfidle.net/vinniejames/m1mesp6z/1/ 但由于某种原因,当父状态更改时,子组件似乎没有更新。 我是不是漏了什么?

  • 问题内容: 是否可以更改流程父级? 例如:父母A有孩子B,我可以使父母B是不杀死A的Init进程吗? 问题答案: 并非来自流程B的外部。 在流程B内部,您可以调用fork来复制您的流程,然后让原始副本退出。发生这种情况时,新副本B2将不是A的子级,其父级将被设置为1(初始化过程)。

  • 假设我有三个UI控制器(A、B、C)。 A是我的根控制器,在ShouldAutoRotate方法中我返回YES。我确实将ModalView从A介绍给B(B= 现在在C中,我可以将模拟器旋转到任何方向,整个视图都可以完美旋转。这就是问题所在,当C是横向的,而我将其忽略时,B中的所有对象都将变得一团糟!!同样的事情也发生在A身上。 我只需要在C上旋转!! 免费。

  • 我想做一些类似于下拉列表的事情,以便我的用户可以选择他们是想要gmail、hotmail还是outlook。然后,我希望按钮更新以显示他们的偏好。我必须只使用引导,因此不能使用 到目前为止,我已经尝试给他们所有相同的id,但是JS只使用了第一个,我不想给他们所有不同的id(太麻烦了)。所以我写的是使用子编号(类似于数组)并将值放入按钮。但是,我不知道如何找出当前html标记的位置号。请帮助我,提前

  • 问题内容: 我正在创建一个程序来跟踪商店库存。我有一个项目名称(字符串)的数组,可以映射以生成一个组件,该组件呈现每个项目的标题以及相应的输入字段: 输出屏幕截图 如何访问输入值及其对应的标题?例如,如果我在输入中输入,则希望同时访问和。 我已经尝试使用(这卷起只参考最后的数组元素),并无济于事。任何建议将不胜感激。 问题答案: 可以为此使用onChange处理程序: 状态现在看起来像这样: