Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当另一个组件的子组件的状态在react中更改时触发事件_Reactjs - Fatal编程技术网

Reactjs 当另一个组件的子组件的状态在react中更改时触发事件

Reactjs 当另一个组件的子组件的状态在react中更改时触发事件,reactjs,Reactjs,假设我有两个组件A和B B的状态不断变化,我想将状态传递给a,以便在满足特定条件时触发事件 如何从React中的另一个组件监视/获取子组件的状态 谢谢 如果您谈论的是父子关系,那么您所要做的就是定义一个函数来更改a上的状态,并将其作为prop传递给B,如下所示: class A extends Component { constructor(props) { super(props); this.state = { changed: false, }

假设我有两个组件A和B

B的状态不断变化,我想将状态传递给a,以便在满足特定条件时触发事件

如何从React中的另一个组件监视/获取子组件的状态


谢谢

如果您谈论的是父子关系,那么您所要做的就是定义一个函数来更改a上的状态,并将其作为prop传递给B,如下所示:

class A extends Component {

  constructor(props) {
    super(props);
    this.state = {
      changed: false,
    }
  }

  _statusChange = () => this.setState({ changed: !this.state.changed });

  render() {
    return(
      <div>
        <span>State on A: { this.state.changed.toString() }</span>
        <B changeHandler={this._statusChange} />
      </div>
    )
  }
}

class B extends Component {

  render() {
    return(
      <div>
        <button onClick={this.props.changeHandler}>Click me</button>
      </div>
    )
  }
}


const App = () => (
    <A />
); 
A类扩展组件{
建造师(道具){
超级(道具);
此.state={
更改为:false,
}
}
_statusChange=()=>this.setState({changed:!this.state.changed});
render(){
返回(
上的状态:{this.State.changed.toString()}
)
}
}
类扩展组件{
render(){
返回(
点击我
)
}
}
常量应用=()=>(
); 

如果它们应该在同一级别上,按照惯例,您应该定义第三个组件,将a和B包装在其中,再次将状态作为它们之间的道具传递

如果您谈论的是父子关系,那么您所要做的就是定义一个函数来更改a上的状态,并将其作为prop传递给B,如下所示:

class A extends Component {

  constructor(props) {
    super(props);
    this.state = {
      changed: false,
    }
  }

  _statusChange = () => this.setState({ changed: !this.state.changed });

  render() {
    return(
      <div>
        <span>State on A: { this.state.changed.toString() }</span>
        <B changeHandler={this._statusChange} />
      </div>
    )
  }
}

class B extends Component {

  render() {
    return(
      <div>
        <button onClick={this.props.changeHandler}>Click me</button>
      </div>
    )
  }
}


const App = () => (
    <A />
); 
A类扩展组件{
建造师(道具){
超级(道具);
此.state={
更改为:false,
}
}
_statusChange=()=>this.setState({changed:!this.state.changed});
render(){
返回(
上的状态:{this.State.changed.toString()}
)
}
}
类扩展组件{
render(){
返回(
点击我
)
}
}
常量应用=()=>(
); 

如果它们应该在同一级别上,按照惯例,您应该定义第三个组件,将a和B包装在其中,再次将状态作为它们之间的道具传递

当组件位于同一级别时:

class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      status: "B not clicked"
    }
    this.componentBchangeHandler = this.componentBchangeHandler.bind(this);
  }
  componentBchangeHandler(value) {
    this.setState({ status: value })
  }
  render() {
    return (
      <div>
        <ComponentA status={this.state.status} />
        <ComponentB componentBchangeHandler={this.componentBchangeHandler} />
      </div>
    )
  }
}

const ComponentA = (props) => <div>{props.status}</div>;
const ComponentB = (props) => <div onClick={() => props.componentBchangeHandler("some val")}>click me</div>;
类父级扩展React.Component{
构造函数(){
超级();
此.state={
状态:“B未单击”
}
this.componentBchangeHandler=this.componentBchangeHandler.bind(this);
}
componentBchangeHandler(值){
this.setState({status:value})
}
render(){
返回(
)
}
}
常量组件a=(props)=>{props.status};
const ComponentB=(props)=>props.componentBchangeHandler(“some val”)}>单击我;


查看我前面提到的文档。

当组件位于同一级别时:

class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      status: "B not clicked"
    }
    this.componentBchangeHandler = this.componentBchangeHandler.bind(this);
  }
  componentBchangeHandler(value) {
    this.setState({ status: value })
  }
  render() {
    return (
      <div>
        <ComponentA status={this.state.status} />
        <ComponentB componentBchangeHandler={this.componentBchangeHandler} />
      </div>
    )
  }
}

const ComponentA = (props) => <div>{props.status}</div>;
const ComponentB = (props) => <div onClick={() => props.componentBchangeHandler("some val")}>click me</div>;
类父级扩展React.Component{
构造函数(){
超级();
此.state={
状态:“B未单击”
}
this.componentBchangeHandler=this.componentBchangeHandler.bind(this);
}
componentBchangeHandler(值){
this.setState({status:value})
}
render(){
返回(
)
}
}
常量组件a=(props)=>{props.status};
const ComponentB=(props)=>props.componentBchangeHandler(“some val”)}>单击我;


查看我前面提到的文档。

您是说子组件和父组件,还是说相同级别的组件?你试过什么?他们应该在同一水平上。我尝试了“componentWillReceiveProps”来获取A中B的状态,但失败了。如果您的组件处于同一级别,您可以使用/或使用公共父级来实现这一点。您是指子级和父级还是指同一级别的组件?你试过什么?他们应该在同一水平上。我尝试了“componentWillReceiveProps”来获取A中B的状态,但失败了。如果您的组件处于同一级别,您可以使用/或使用公共父级来实现这一点。谢谢您的回答。如果我想让B向外部传递一个值呢?类似这样的东西:componentBchangeHandler(value){this.setState({status:value})}这个答案有用吗?谢谢你的回答。如果我想让B向外部传递一个值呢?类似这样的东西:componentBchangeHandler(value){this.setState({status:value})}这个答案有用吗?