【问题标题】:React Material-UI TextField looses focus with every keyReact Material-UI TextField 失去焦点与每个键
【发布时间】:2020-02-14 16:23:32
【问题描述】:

我有一个 ma​​terial-ui CharField,但我无法输入完整的字符串,因为它在输入一个键后失去焦点,所以我必须在每次击键后单击它,即不可接受。

这是我的组件SearchControl:

render() {
    const SearchControl = () => {
      return (
        <div className="col-6">
          <TextField
            className="col-11"
            id={this.state.code}
            defaultValue={this.state.code}
            label={"Modelo o Número de Serie"}
            variant="outlined"
            onChange={this.handleChange.bind(this)}
          />
          <IconButton
            ariaLabel="Buscar"
            className="col-1"
            onClick={this.handleSearchProductClick}
          >
            <SearchIcon />
          </IconButton>
        </div>
      );
    };

    if (!this.state.messageShown) {
      return (
        <div>
          <div style={{ display: "flex" }}>
            <NavBar onUpdate={this.onUpdate} />
          </div>
        </div>
      );
    } else {
      return (
        <div>
          <div fullWidth style={{ display: "flex", paddingTop: "30px" }}>
            <SearchControl />
            <ShortcutButtons />
          </div>
          <div fullWidth>
            <ProductsTable />
          </div>
          <div fullWidth />
          <div fullWidth style={{ display: "flex", paddingTop: "10px" }}>
            <ControlButtons />
            <TotalsDisplay />
          </div>
        </div>
      );
    }
}

【问题讨论】:

  • 尝试给 id 一个非状态值。
  • 我以前有过这个,你的父母正在重新安装更改。您需要确保不重新渲染父组件。
  • 要对此进行测试,请在您的@987654323 上添加一个console.log@这应该显示每次都重新安装组件
  • @SirajAlam 没用。
  • 尝试将 value 属性作为存储在 state 中的当前值传递。

标签: reactjs material-ui


【解决方案1】:

我找到了解决方案。问题是 SearchControl 组件是一个箭头函数,这意味着每次渲染它都是一个全新的函数,因此每次渲染都是一个全新的组件。

我将 SearchControl 组件声明更改为常规函数,因此每次渲染都将引用同一个函数,因此也指向同一个组件:

constructor(props) {
    super(props);

    this.SearchControl = this.SearchControl.bind(this);
}

SearchControl() {
      return (
        <div className="col-6">
          <TextField
            className="col-11"
            id={this.state.code}
            defaultValue={this.state.code}
            label={"Modelo o Número de Serie"}
            variant="outlined"
            onChange={this.handleChange.bind(this)}
          />
          <IconButton
            ariaLabel="Buscar"
            className="col-1"
            onClick={this.handleSearchProductClick}
          >
            <SearchIcon />
          </IconButton>
        </div>
      );
    }

render() {
   if (!this.state.messageShown) {
      return (
        <div>
          <div style={{ display: "flex" }}>
            <NavBar onUpdate={this.onUpdate} />
          </div>
        </div>
      );
    } else {
      return (
        <div>
          <div fullWidth style={{ display: "flex", paddingTop: "30px" }}>
            <this.SearchControl />
            <ShortcutButtons />
          </div>
          <div fullWidth>
            <ProductsTable />
          </div>
          <div fullWidth />
          <div fullWidth style={{ display: "flex", paddingTop: "10px" }}>
            <ControlButtons />
            <TotalsDisplay />
          </div>
        </div>
      );
    }
  }

【讨论】:

猜你喜欢
  • 2019-01-23
  • 1970-01-01
  • 2021-02-18
  • 2020-05-27
  • 2021-06-18
  • 2020-05-24
  • 1970-01-01
  • 2017-03-01
  • 1970-01-01
相关资源
最近更新 更多