简体   繁体   English

在 React 组件中生成输入

[英]Generating inputs in a React component

I am trying to generate inputs on a button click and the amount of inputs is generated by a random number.我正在尝试在单击按钮时生成输入,并且输入量是由随机数生成的。 Here is what I have so far, but it isn't working.这是我到目前为止所拥有的,但它不起作用。 I am very confused and feel like it should be working.我很困惑,觉得它应该工作。 I am not sure what I am missing.我不确定我错过了什么。 Any help would be very much appreciated.任何帮助将不胜感激。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import { Image } from './Image.js'
import { Button } from './Button.js'
import { images } from './assets/images.js'
import { Countdown } from './Countdown.js'
import { DisplayCount } from './DisplayCount.js'
import { Inputs } from './Inputs.js'

class Game extends React.Component{
  constructor(props){
    super(props)

    this.timer = null


    this.state = {
      currentImg: 0,
      timer: null,
      ranNum: null
    }

    this.handleClick = this.handleClick.bind(this)
  }

countdownClock = async (newRanNum) => {
const startingNum = newRanNum * 20;
for(let i = startingNum; i >= 0; i--) {
    await new Promise(resolve => {
        this.timer = setTimeout(() => {
         this.setState({
           timer: i
         })
        resolve()
     }, 1000)
   });
  }
}

generateInputs = (newRanNum) => {
    const inputs = []
    for(let i = 1; i <= newRanNum; i++){
      inputs.push(
        <Inputs type='text'   className='textInputs' />
      )
    }
    return inputs;
  }

  handleClick(){
    clearTimeout(this.timer)
    let newRanNum = Math.floor(Math.random() * 20);
    this.countdownClock(newRanNum)
    this.generateInputs(newRanNum)
    let current = this.state.currentImg;
    let next = ++current % images.length;
    this.setState({
      currentImg: next,
      ranNum: newRanNum
    })
  }

  render(){
    let src = this.state.currentImg;
    return(
      <div>
        <Countdown name={'Countdown: '} countdown={this.state.timer} />
        <DisplayCount name='Word Count: ' count={this.state.ranNum} />
        <Image src={images[src]} />
        <Button onClick={this.handleClick} />
        <div>
          <ul>
          {this.generateInputs()}
          </ul>
        </div>

      </div>
    )
  }
}


ReactDOM.render(
  <Game />,
document.getElementById('root')
);

Inputs component:输入组件:

import React from 'react'

export const Inputs = (props) => {
    return (
      <li className={props.className}>
        <input value={props.value}  />
      </li>
    )
}

The first thing is your generateInputs() function takes a parameter for number of inputs you wanted to render but you are not passing any parameter to this.generateInputs()首先是您的generateInputs() function 需要一个参数来表示您想要渲染的输入数量, but you are not passing any parameterthis.generateInputs()

The second thing is you are returning an array from this function but not mapping your array in the render function, So do in this way.第二件事是你从这个 function 返回一个数组,但没有在渲染 function 中映射你的数组,所以以这种方式做。

this.generateInputs(10) // pass parameter here


this.generateInputs(10).map((item, index)=>item)

In this Snack, I have done the same thing but with react-native在这个小吃中,我做了同样的事情,但使用 react-native

https://snack.expo.io/@waheed25/smiling-carrot https://snack.expo.io/@waheed25/smiling-carrot

I believe the issue is here...我相信问题就在这里...

generateInputs = (newRanNum)...

and here...和这里...

{this.generateInputs()}

You're input rendering function is expecting a parameter it's not getting, and since that returns as undefined the loop never runs.您正在输入渲染 function 期待一个它没有得到的参数,并且由于它返回为undefined的循环永远不会运行。 :) :)

In general, it is most common to separate state (eg, number of inputs) from the rendering, and only have the rendering respond to state.通常,最常见的做法是将state (例如,输入的数量)与渲染分开,并且仅让渲染响应state。 Perhaps you had originally intended generateInputs() to produce the array, and not render them (?)也许您最初打算generateInputs()来生成数组,而不是渲染它们(?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM