1

在组件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状态并没有改变。

4

1 回答 1

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

handleCodex(codex){      
  this.setState({
      code: codex,
    });  // this.setState is like a function call. Not a assignment statement.
}


<BlocklyDrawer
      tools={[INICIAR, MOVER, ATACAR]}
      language = {Blockly.Javascript}
      onChange={(code, workspace) => {                       
        this.handleCodex(code);
      }}
      appearance={
        {
          categories: {
            Agente: {
              colour: '160'
            },
          },
        }
      }
    >
于 2018-08-07T18:51:05.973 回答