javascript - Reactjs 只能更改输入中的一个字母?

标签 javascript reactjs mobx

我在 Reactjs 中有一个类,如下所示:

 class ViewLegos extends Component {
  componentDidMount() {
    this.props.store.getAllLegoParts();
  }

  render() {
    const columns = [
      {
        Header: "Piece",
        accessor: "piece"
      },
      {
        Header: "Type",
        accessor: "type"
      }
    ];
    return (
      <div>
        {this.props.store.legoParts.state === "pending" ? (
          <h3>No data to display!</h3>
        ) : (
          <ReactTable
            defaultPageSize={10}
            className="-striped -highlight"
            data={this.props.store.legoParts.value}
            columns={columns}
            SubComponent={row => {
              console.log(row);
              return (
                <Table striped bordered condensed hover>
                  <thead>
                    <tr>
                      <th>Name</th>
                      <th>Type</th>
                      <th>Start Date</th>
                      <th>End Date</th>
                      <th>Change it up!</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr onClick={this.props.store.changeData(row.original)}>
                      <td>
                        <input
                          type="text"
                          onChange={e => this.props.store.addLego("piece", e)}
                          name="piece"
                          value={row.original.piece}
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          onChange={e =>
                            this.props.store.addLego("type", e.target.value)
                          }
                          placeholder={this.props.store.type}
                        />
                      </td>
                      <td>
                        <Datetime
                          date="date"
                          onChange={e =>
                            this.props.store.addLego("startDate", e)
                          }
                        />
                      </td>
                      <td>
                        <Datetime
                          type="date"
                          onChange={e => this.props.store.addLego("endDate", e)}
                        />
                      </td>
                      <td>
                        <button
                          onClick={() =>
                            this.props.store.updatePiece(row.original)
                          }
                        >
                          Update
                        </button>
                        <button
                          onClick={() =>
                            this.props.store.deletePiece(
                              this.props.row.original.id
                            )
                          }
                        >
                          Delete
                        </button>
                      </td>
                    </tr>
                  </tbody>
                </Table>
              );
            }}
          />
        )}
      </div>
    );
  }
}

单击一行时调用的changeData 函数位于MobX 存储中,如下所示:

changeData = action("change state", part => {
    this.piece = part.piece;
  });

当输入字段中的值发生更改时,Mobx 存储中会启用此功能:

 addLego = action("addLego", (storeName, value) => {
    this[storeName] = value.target.value;
  });

我希望能够更新输入字段中的值。不过目前我只能改一个字母!每次我输入更多字母时,一个字母就会变成新字母。感谢您的帮助!

最佳答案

您在渲染时立即调用 changeData 函数。改为给 onClick 一个函数:

<tr onClick={() => this.props.store.changeData(row.original)}>

关于javascript - Reactjs 只能更改输入中的一个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48136892/

相关文章:

javascript - 登录时重定向至主页,并在 url 中附加用户名

javascript - 如何在 vuetify v-select 中使用图标?

javascript - WebRTC音频无法播放-间歇性

javascript - 使用 ReactJS 和 fetch-api 访问和显示 JSON 时遇到问题

javascript - 如何获取 JSON 对象到 React 组件?

javascript - 使用 javascript API 响应内部服务器错误将轨道上传到 SoundCloud

reactjs - 对于基于类的组件,useRouteMatch 的等价物是什么?

javascript - MobX - 替换可观察数组中的项目?

javascript - react 错误index.js :1452 Warning: Each child in an array or iterator should have a unique "key" prop

reactjs - 如何以 react 方式从 mobx observable 获取项目?