How to set React component state and props from browser

后端 未结 3 1505
遥遥无期
遥遥无期 2021-02-07 10:10

Is there a possibility to access and set the state and props of an existing React component through the browser (from inside another script or through console)?

I know t

3条回答
  •  走了就别回头了
    2021-02-07 11:00

    To set the state from your browser, stick this somewhere in your component, like in render():

      globalSetState = function(state){this.setState(state)}.bind(this);
    

    Note: It's important to leave off the var, as this is what makes the defined function globally accessible.

    You can now call globalSetState({x: 'y'}) in your console.

    Warning: This is mad ugly, and, like console.log() for debugging, should be deleted in your live app.

提交回复
热议问题