【问题标题】:React Component is not rerendering on setStateReact 组件没有在 setState 上重新渲染
【发布时间】:2020-01-22 21:53:31
【问题描述】:

我有以下代码。我正在使用 setState 方法 (this.setState({ country: "", offset: "" });) 设置状态,它不会导致 render() 方法自行重新呈现。

  state = { country: "", offset: "" };
  onClicked = () => {
    console.log("Fine Click away");
    // Get the call back from props
    let country = this.state.country;
    let offset = this.state.offset;
    this.setState({ country: "", offset: "" });
    const callback = this.props.onpress;

    if (callback) {
      callback(country, offset);
    }
  };
getTheCountryName = event => {
    this.setState({ country: event.target.value });
  };
  getTheOffSet = e => {
    this.setState({ offset: e.target.value });
  };
render() {

    const Caption = this.props.caption;
    return (
      <div>
        <br></br>
        <br></br>
        <div>Please enter the name of country and offset:</div>
        <input
          placeholder="Name of Country"
          onChange={this.getTheCountryName}
        />
        <input placeholder="offset" onChange={this.getTheOffSet} />
        <br></br>
        <br></br>
        <button className="rectangular-button" onClick={this.onClicked}>
          <div className="caption">{Caption}</div>
        </button>
      </div>
    );
  }
}```

【问题讨论】:

  • 不确定为什么要复制状态属性的值。
  • 你怎么知道render没有被调用?您不会在组件的 state 中显示值。
  • 您的代码确实触发了render 调用:codesandbox.io/s/divine-cloud-vv3ob

标签: javascript html css reactjs jsx


【解决方案1】:

以任何方式重置状态都不会触发输入值的更新,因为您实际上并没有在返回的 JSX 中使用您的状态。下面的代码会将值提供给输入,以便在您的状态更改时更新它们。你可以阅读 React here 中的表单和受控输入:

class App extends React.Component {
   state = { country: "", offset: "" };
  onClicked = () => {
    console.log("Fine Click away");
    // Get the call back from props
    let country = this.state.country;
    let offset = this.state.offset;
    this.setState({ country: "", offset: "" });
    const callback = this.props.onpress;

    if (callback) {
      callback(country, offset);
    }
  };
getTheCountryName = event => {
    this.setState({ country: event.target.value });
  };
  getTheOffSet = e => {
    this.setState({ offset: e.target.value });
  };
render() {

    const Caption = this.props.caption;
    return (
      <div>
        <br></br>
        <br></br>
        <div>Please enter the name of country and offset:</div>
        <input
          placeholder="Name of Country"
          onChange={this.getTheCountryName}
          value={this.state.country}
        />
        <input value={this.state.offset} placeholder="offset" onChange={this.getTheOffSet} />
        <br></br>
        <br></br>
        <button className="rectangular-button" onClick={this.onClicked}>
          <div className="caption">Reset</div>
        </button>
      </div>
    );
  }
}

【讨论】:

  • 单击该按钮确实会触发重新渲染,正如您在我上面评论中链接的沙箱中看到的那样。
  • 你是对的。更准确地说,它不会更新输入的值。我已经更新了我的答案。
猜你喜欢
  • 2019-08-10
  • 1970-01-01
  • 2021-04-28
  • 2018-01-30
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2019-09-26
  • 2015-06-30
相关资源
最近更新 更多