【问题标题】:ReactJS input type = "text" maxLength is not working when passed as propsReactJS input type = "text" maxLength 在作为道具传递时不起作用
【发布时间】:2019-10-01 16:23:19
【问题描述】:

我正在尝试将 maxLength 作为道具传递给,但它不起作用。任何人都可以帮忙吗?谢谢!

以下是两个组件:

 <Input
        id="creategroup-group-and-description-textfield"
        value={value}
        type="text"
        multiline={this.props.multiline}
        rowsMax={this.props.rowsMax}
        maxLength={this.props.maxLength}
        onChange={this.handleInputChange}
        placeholder={this.props.placeholder}
      />



     <TextFieldWithTitle
          title="Group Name"
          maxLength={3}
          multiline={false}
          placeholder="e.g.Study and Workout"
        />

【问题讨论】:

  • 你能分享你的2个组件的代码吗?
  • 嗨!考虑将代码 sn-ps 添加到您的问题而不是他们的图像中。
  • 我分享了上面的代码
  • InputTextFieldWithTitle 是什么?样式化的组件?你自己的组件?还是什么?
  • TextFieldWithTitle 是我定义的组件。 是 Material UI 中的组件。这两个组件位于不同的文件中。 位于 TextFieldwithTitle 的渲染部分,它是另一个主组件的子组件。我在主组件文件中调用了 TextFieldWithTitle .. 将道具传回子组件文件

标签: reactjs input react-props maxlength


【解决方案1】:

我终于想出了答案,决定把它放在这里给以后可能有同样问题的人:)

回答: 如果你想将 maxLength 作为 React 中的道具传递。你应该使用:

inputprops = {{maxLength = num}} (note: num can be any positive num you want. No {} and "" needed.

然后,在收到这个道具的地方,写:

inputprops = {this.props.inputprops} 

要限制输入长度,写:

<Input 
inputprops={{maxLength: 100(e.g.)}}
/>

注意:Inputprops 和 inputprops 在 React 中是两个不同的东西。小心点。

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 2018-08-02
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多