Reactjs 在React中禁用基于更改值的按钮

Reactjs 在React中禁用基于更改值的按钮,reactjs,redux,Reactjs,Redux,如果this.state.title少于3个字符,我将尝试使用onChange将更新按钮上的禁用按钮设置为true,但它不允许我输入值。它几乎破坏了密码。我希望将更新传递给redux 以下是我所做的,免责声明一些代码已被删除,以便更相关 positem.js 柱减速器用于禁用按钮等 可编辑组件 您不需要调用DisableButton方法来禁用其值 修复onChange,大致如下所示: onChange = (e) => { this.setState({ title: e.target

如果this.state.title少于3个字符,我将尝试使用onChange将更新按钮上的禁用按钮设置为true,但它不允许我输入值。它几乎破坏了密码。我希望将更新传递给redux

以下是我所做的,免责声明一些代码已被删除,以便更相关

positem.js

柱减速器用于禁用按钮等

可编辑组件

您不需要调用DisableButton方法来禁用其值

修复onChange,大致如下所示:

onChange = (e) => {
  this.setState({ title: e.target.value });
}
<Button
  disabled={this.state.title.length <= 3}
  // rest of the attributes
/>
<Button
  disabled={this.props.myTitle.length <= 3}
  // rest of the attributes
/>
是的,这就足够了

现在可以使用逻辑禁用按钮组件,如下所示:

onChange = (e) => {
  this.setState({ title: e.target.value });
}
<Button
  disabled={this.state.title.length <= 3}
  // rest of the attributes
/>
<Button
  disabled={this.props.myTitle.length <= 3}
  // rest of the attributes
/>
===在提问者添加一些细节后第二次编辑

如果this.state.title位于PostList上,则父组件和您的按钮将通过myTitle道具访问它,因为它位于PostItem组件上。您可以这样做:

onChange = (e) => {
  this.setState({ title: e.target.value });
}
<Button
  disabled={this.state.title.length <= 3}
  // rest of the attributes
/>
<Button
  disabled={this.props.myTitle.length <= 3}
  // rest of the attributes
/>