【问题标题】:Trouble passing React prop to DOM element将 React 道具传递给 DOM 元素时遇到问题
【发布时间】:2018-09-28 14:43:52
【问题描述】:

我正在尝试将输入限制为整数。我正在尝试传递直接在 HTML 中使用时似乎可以工作的 step 属性,但是很难让它从反应中传递出去。下面是我的渲染方法,数字输入在底部。我遇到的问题是输入仍然接受十进制。

render() {
    const {
      currentVariableValue,
      type,
      currentVariable,
      variablePayload,
    } = this.props

    const lowHi = this.getLowHi()
    const lowHiDisplay = `between ${lowHi[0]} and ${lowHi[1]}`
    /* eslint-disable no-nested-ternary */
    return (
      <ResponseContainer>
        {
          type === 'text' ?
            <TransparentTextArea
              autoFocus
              type="text"
              value={ currentVariableValue }
              onChange={ this.handleUpdateValue }
              onFocus={ this.handleFocus }
              onKeyPress={ this.handleKeyPress }
              key={ currentVariable }
            /> :
            type === 'paragraph' ?
              <TransparentTextArea
                autoFocus
                type="text"
                value={ currentVariableValue }
                onChange={ this.handleUpdateValue }
                onFocus={ this.handleFocus }
                onKeyPress={ this.handleKeyPress }
                key={ currentVariable }
                style={ { height: '300px', width: '95%', fontSize: '1.4em' } }
              /> :
              <TransparentInput
                autoFocus
                type="numeric"
                value={ currentVariableValue }
                onChange={ this.handleUpdateValue }
                onFocus={ this.handleFocus }
                onKeyPress={ this.handleKeyPress }
                key={ currentVariable }
                invalid={ !variablePayload.isValid }
                step={ 1 }
              />
        }

【问题讨论】:

  • >让它通过 react
  • 问题是该字段仍然接受十进制值。

标签: javascript html reactjs react-templates


【解决方案1】:

错误在您的 type 属性中。 将其更改为number。喜欢:

<TransparentInput
  autoFocus
  type="number"

【讨论】:

    【解决方案2】:

    在您的透明输入中,您是否将 {...props} 传递给输入元素?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 2020-09-07
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      相关资源
      最近更新 更多