首页 > 解决方案 > 将 React 道具传递给 DOM 元素时遇到问题

问题描述

我正在尝试将输入限制为整数。我正在尝试传递直接在 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 }
              />
        }

标签: javascripthtmlreactjsreact-templates

解决方案


错误在你的type道具中。将其更改为number. 喜欢:

<TransparentInput
  autoFocus
  type="number"

推荐阅读