Javascript 我可以在ReactJs中修改组件的父状态吗?

Javascript 我可以在ReactJs中修改组件的父状态吗?,javascript,reactjs,Javascript,Reactjs,假设我有以下组件: class LeftPanel extends React.Component { constructor(props) { super(props); this.state = {abc: 'xyz'}; } render() { return ( <div> <Thumb src={img1}/> <Thumb src={img2}/> </div>); }

假设我有以下组件:

class LeftPanel extends React.Component {
  constructor(props) {
    super(props);
    this.state = {abc: 'xyz'};
  }
render() {
  return (
    <div>
     <Thumb src={img1}/>
     <Thumb src={img2}/>
   </div>);
 }
class LeftPanel扩展了React.Component{
建造师(道具){
超级(道具);
this.state={abc:'xyz'};
}
render(){
返回(
);
}
在组件内部,有一个名为Thumb的内部组件,如下所示:

class Thumb extends React.Component {
  constructor(props) {
   super(props);
   this.state = { src: props.src };
  }

  render() {
   return (
    <div>
      <img src={this.state.src}></img>
    </div>
   );
  }
class.Component{
建造师(道具){
超级(道具);
this.state={src:props.src};
}
render(){
返回(
);
}
}


问题:我可以在Thumb中修改LeftPanel组件的状态吗?如果可以,如何在LeftPanel中创建一个方法来处理事件。然后将该方法传递给子组件

现在,如果您想将数据传递给父级,我建议使用像flux或redux这样的库。因为redux具有全局状态,所以所有组件都可以访问它。当您将全局状态修改为整个应用程序时,将使用新状态重新提交。所有组件都可以看到新状态

class LeftPanel extends React.Component {
  constructor(props) {
    super(props);
    this.modifyLeftpanel = this.modifyLeftpanel.bind(this);
    this.state = {abc: 'xyz'};
  }

  modifyLeftpanel(newvar){
      //Do something with newvar
  }
render() {
  return (
    <div>
     <Thumb src={img1} modifyLeftPanel={this.modifyLeftpanel}  />
     <Thumb src={img2} modifyLeftPanel={this.modifyLeftpanel} />
   </div>);
 }
class LeftPanel扩展了React.Component{
建造师(道具){
超级(道具);
this.modifyLeftpanel=this.modifyLeftpanel.bind(this);
this.state={abc:'xyz'};
}
modifyLeftpanel(新变量){
//用newvar做点什么
}
render(){
返回(
);
}
这是拇指

class Thumb extends React.Component {
  constructor(props) {
   super(props);
   this.state = { src: props.src };
   this.doSomething = this.doSomething.bind(this);
  }

  doSomething(){
       this.props.modifyLeftpanel(10);
  }

  render() {
   return (
    <div>
      <img src={this.state.src}></img>
      <button onClick={this.doSomething}>Modify Parent</button>
    </div>
   );
  }
class.Component{
建造师(道具){
超级(道具);
this.state={src:props.src};
this.doSomething=this.doSomething.bind(this);
}
doSomething(){
此.props.modifyLeftpanel(10);
}
render(){
返回(
修改父项
);
}

在左侧面板中创建一个方法来处理事件。然后将该方法传递给子组件

现在,如果您想将数据传递给父级,我建议使用像flux或redux这样的库。因为redux具有全局状态,所以所有组件都可以访问它。当您将全局状态修改为整个应用程序时,将使用新状态重新提交。所有组件都可以看到新状态

class LeftPanel extends React.Component {
  constructor(props) {
    super(props);
    this.modifyLeftpanel = this.modifyLeftpanel.bind(this);
    this.state = {abc: 'xyz'};
  }

  modifyLeftpanel(newvar){
      //Do something with newvar
  }
render() {
  return (
    <div>
     <Thumb src={img1} modifyLeftPanel={this.modifyLeftpanel}  />
     <Thumb src={img2} modifyLeftPanel={this.modifyLeftpanel} />
   </div>);
 }
class LeftPanel扩展了React.Component{
建造师(道具){
超级(道具);
this.modifyLeftpanel=this.modifyLeftpanel.bind(this);
this.state={abc:'xyz'};
}
modifyLeftpanel(新变量){
//用newvar做点什么
}
render(){
返回(
);
}
这是拇指

class Thumb extends React.Component {
  constructor(props) {
   super(props);
   this.state = { src: props.src };
   this.doSomething = this.doSomething.bind(this);
  }

  doSomething(){
       this.props.modifyLeftpanel(10);
  }

  render() {
   return (
    <div>
      <img src={this.state.src}></img>
      <button onClick={this.doSomething}>Modify Parent</button>
    </div>
   );
  }
class.Component{
建造师(道具){
超级(道具);
this.state={src:props.src};
this.doSomething=this.doSomething.bind(this);
}
doSomething(){
此.props.modifyLeftpanel(10);
}
render(){
返回(
修改父项
);
}

是的,在
LeftPanel
中添加一个修改状态的方法,然后将该方法作为道具传递给
Thumb
。已经有了。还有,为什么要将道具复制到Thumb中的状态?这看起来是个有问题的模式,你不应该这样做。我相信你的问题在本文档中得到了回答。你有机会阅读它吗然而?@DanAbramov我开始使用React,谢谢你的建议。我不知道这样将值传递到状态是一种不好的做法。是的,在
LeftPanel
中添加一个修改状态的方法,然后将该方法作为道具传递到
Thumb
。已经有了。你为什么要将道具复制到Thumb中的状态?这看起来很像e一个有问题的模式,你不应该这样做。我相信你的问题在这份文件中得到了回答。你有机会读过吗?@DanAbramov我开始使用React,谢谢你的建议。我不知道这样向国家传递价值观是一个不好的做法。