React组件不会更改父级的状态

时间:2018-08-07 17:04:23

标签: reactjs blockly

在组件BlocklyDrawer中,我试图更改父组件的code状态。我在onChange()事件中执行此操作,调用了父组件handleCodex的方法:

   constructor(props) {
    super(props);
    this.state = {
      code : "xxx",
    }; 
       this.handleCodex = 
        this.handleCodex.bind(this);
  }

handleCodex(codex){      
  this.setState = ({
      code: codex,
    });
}


<BlocklyDrawer
      tools={[INICIAR, MOVER, ATACAR]}
      language = {Blockly.Javascript}
      onChange={(code, workspace) => {                       
        this.handleCodex(code);
      }}
      appearance={
        {
          categories: {
            Agente: {
              colour: '160'
            },
          },
        }
      }
    >

尽管执行了方法handleCodex,但code状态不变。

1 个答案:

答案 0 :(得分:1)

var elem = document.getElementById("SubmitBtn")
var click = new MouseEvent('click', {
    view: window
});
elem.dispatchEvent(click);