简体   繁体   English

React.js组件甚至不呈现状态更改

[英]React.js component not rendering even state changes

The alert modal dialog don't change even state changes 警报模式对话框即使状态更改也不改变

I shared codepen here . 在这里共享了Codepen。 This is component. 这是组件。

class RecordingReports extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      dialog: null,
      newContent: {
        contentType: "recording"
      }
    };
  }
  toggleContentType = () => {
    let newContent = this.state.newContent;
    this.setState({
      newContent: {
        ...newContent,
        contentType:
          newContent.contentType == "recording" ? "report" : "recording"
      }
    });
  };
  showDialog = () => {
    this.setState({
      dialog: (
        <div>
          <button
            onClick={() => {
              this.toggleContentType();
            }}
          >
            {this.state.newContent.contentType} // it remains recording
          </button>
        </div>
      )
    });
  };
  render() {
    console.log(this.state.newContent); //this shows state changes
    return (
      <div>
        {this.state.dialog} 
        <button onClick={() => this.showDialog()} >show Toggle Button</button>
      </div>
    );
  }
}

To test first click show toggle, and there you see button with title recording , I want this to toggle when I click but it never do. 要测试第一次单击“显示切换”,然后您会看到带有标题recording按钮,我希望在单击时切换它,但是从不这样做。

I shared codepen here . 在这里共享了Codepen。

If you see console, you see it changes state. 如果您看到控制台,则会看到它更改状态。

I am not sure what is wrong, 我不知道怎么了

Thanks 谢谢

You do not need dialog in state, it will not change in the dialog, this will not change as your are not setting state. 您不需要处于状态的对话框,它不会在对话框中更改,这也不会更改,因为您未设置状态。 that onclick on dialog is again setting the same state without changing the record type. 对话框上的onclick再次设置了相同的状态,而没有更改记录类型。

problem: 问题:

this.setState({
      dialog: (
        <div>
          <button
            onClick={() => {
              this.toggleContentType();
            }}
          >
            {this.state.newContent.contentType} // it remains recording
          </button>
        </div>
      )
    });

Solution: 解:

constructor(props) {
    super(props);

    this.state = {
      dialog: null,
      newContent: {
        contentType: "recording"
      },
      showDialog: false
    };
  }
  toggleContentType = () => {
    let newContent = this.state.newContent;
    this.setState({
      newContent: {
        ...newContent,
        contentType:
          newContent.contentType === "recording" ? "report" : "recording"
      }
    });
  };
  dialog = () => {
    return this.state.showDialog && <div>
      <button
        onClick={() => {
          this.toggleContentType();
        }}
      >
        {this.state.newContent.contentType} // it remains recording
      </button>
    </div>

  };

  showHideDialog = () => {
    this.setState({ showDialog: !this.state.showDialog });
  }
  render() {

    return (
      <div>
        {this.dialog()}
        <button onClick={() => this.showHideDialog()} >show Toggle Button</button>
      </div>
    );
  }

demo 演示

Yes it won't change because the whole element inside dailog state is not component just javascript elements, 是的,它不会改变,因为dailog状态内的整个元素不只是javascript元素,

Solution is separate Dialog as a another component and pass props, this way your code will be more clean and readable. 解决方案是将单独的Dialog作为另一个组件并传递道具,这样您的代码将更加清晰易读。

 const Dialog = ({ show, onClick, newContent }) => { return !show ? null : ( <div> <button onClick={onClick}>{newContent.contentType}</button> </div> ); }; class RecordingReports extends React.Component { constructor(props) { super(props); this.state = { showDialog: false, newContent: { contentType: "recording" } }; } toggleContentType = () => { let newContent = this.state.newContent; this.setState({ newContent: { ...newContent, contentType: newContent.contentType == "recording" ? "report" : "recording" } }); }; toggleDialog = () => { this.setState({ showDialog: !this.state.showDialog }); }; render() { console.log(this.state.newContent); //this shows state changes return ( <div> <Dialog show={this.state.showDialog} onClick={this.toggleContentType} newContent={this.state.newContent}/> <button onClick={this.toggleDialog}>show Toggle Button</button> </div> ); } } ReactDOM.render(<RecordingReports />, document.getElementById("root")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"><div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM