javascript - 响应变量值更改不更新 React 组件

标签 javascript reactjs

更新 react 变量值 onClick 但不会更新 react 组件 我不明白为什么它不起作用以及问题到底是什么。

代码

constructor(props) {
  super(props);
  this.handleSubmit = this.handleSubmit.bind(this);
  this.state.pageEditInitialValue = false;
}

handleSubmit = id => {
  alert(id);
  this.state.pageEditInitialValue = true;
};

render() {
  const pageEdit = this.state.pageEditInitialValue;
  return (
    <div>
      {pageEdit ? (
        <Page title="Configuration">
          <Row>
            <Col>
              <Card className="mb-3">
                <CardHeader>Hotel Edit</CardHeader>
                <CardBody>
                  <Table responsive>
                    <thead>
                      <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Rating</th>
                        <th>Location</th>
                        <th />
                      </tr>
                    </thead>
                    <tbody>
                      {this.state.persons.map((person, index) => (
                        <tr key={person.hotelId}>
                          <td> {person.hotelId}</td>
                          <td> {person.hotelName}</td>
                          <td> {person.hotelRating}</td>
                          <td> {person.hotelLocation}</td>
                          <td>
                            <Button
                              color="primary"
                              onClick={() => this.handleSubmit(person._id)}
                            >
                              Edit
                            </Button>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </Table>
                </CardBody>
              </Card>
            </Col>
          </Row>
        </Page>
      ) : (
        <Page title="Configuration">
          <Row>
            <Col>
              <Card className="mb-3">
                <CardHeader>Hotel Data</CardHeader>
                <CardBody>
                  <Table responsive>
                    <thead>
                      <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Rating</th>
                        <th>Location</th>
                        <th />
                      </tr>
                    </thead>
                    <tbody>
                      {this.state.persons.map((person, index) => (
                        <tr key={person.hotelId}>
                          <td> {person.hotelId}</td>
                          <td> {person.hotelName}</td>
                          <td> {person.hotelRating}</td>
                          <td> {person.hotelLocation}</td>
                          <td>
                            <Button
                              color="primary"
                              onClick={() => this.handleSubmit(person._id)}
                            >
                              Edit
                            </Button>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </Table>
                </CardBody>
              </Card>
            </Col>
          </Row>
        </Page>
      )}
    </div>
  );
}

最佳答案

您不应该直接改变组件状态,而应该调用setState method相反。

handleSubmit = (id) => {
  alert(id)
  this.setState({ pageEditInitialValue: true })
}

关于javascript - 响应变量值更改不更新 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535747/

相关文章:

javascript - Kendo : How do I create a recursive hieiarchy in the TreeView?

javascript - 获取与 react-dom 相关的错误

javascript - 使用 FullCalendar、jQuery 和 MVC 查看数据库中的数据

windows - 如何使用 JAVAScript 或 VBScript 控制 Windows 系统音量?

javascript - Lodash 查找范围而不是数字的精确匹配

javascript - 遇到两个拿着同把 key 的 child

javascript - 使用动态内容 react Swiper

javascript - 如何在 Reactjs 中使用 Select2?

javascript - 如何让JQuery json函数在javascript之前运行

javascript - 防止 RxJS 中异步可管道运算符过早完成