简体   繁体   English

无法在 React 中检索 prop 的值

[英]Not able to retrieve value of prop in React

First time posting on here, so hopefully I explain this right...I'm building a multi-step form in React, and I want to display a variable number of input fields in one of those steps.第一次在这里发帖,所以希望我能正确解释这一点......我正在 React 中构建一个多步骤表单,我想在其中一个步骤中显示可变数量的输入字段。 The number of fields required is set in the state of a class component.所需字段的数量在类组件的状态中设置。

The code works as planned when I just enter a number ie renders what I want it to, but not when I try to declare 'num' as props.number当我只输入一个数字时,代码按计划工作,即呈现我想要的内容,但当我尝试将 'num' 声明为 props.number 时则不然

It doesn't throw an error, just displays nothing.它不会抛出错误,只是什么都不显示。 Seems to retrieve the value of props.currentStep just fine.似乎检索 props.currentStep 的值就好了。 In fact, if i let num = props.currentStep, it renders the corresponding number of fields.事实上,如果我让 num = props.currentStep,它会呈现相应数量的字段。

    function Step2(props) {
      let num = props.number;
      let i = 0
      if (props.currentStep !== 2) {
        return null
      } 
      return(
        <React.Fragment>
          <div>
            {Array(num).fill().map(() => {
               i += 1
               return (
                 <div key={i}>
                   <TextField 1>
                   <TextField 2>
                 </div>
               )
             })
           }     
         </div>
       </React.Fragment>         
     )
   }

I also tried using a getter in the class component, and that logs the number correctly to the console, but I still can't get the value and use this in my function.我还尝试在类组件中使用 getter,并将数字正确记录到控制台,但我仍然无法获取该值并在我的函数中使用它。

class Form extends Component {

  constructor(props) {
    super(props)

    this.state = {
      currentStep: 1,
      number: 3,

      get Number() {
        let result = this.number;
        console.log(result);
        return result
      }
    }
  }

  render() {
    return (
      <div>
        <React.Fragment>       
          <form>     
            <Step1 
              currentStep={this.state.currentStep} 
            />
            <Step2 
              currentStep={this.state.currentStep} 
            />
            <Step3 
              currentStep={this.state.currentStep} 
            />
          </form>
        </React.Fragment>
      </div>
    )
  }
}

Even with the getter, let num = props.Number does not work.即使使用 getter, let num = props.Number 也不起作用。

I feel like I'm missing something obvious, or maybe just doing this completely the wrong way.我觉得我错过了一些明显的东西,或者可能只是以完全错误的方式做这件事。

Trying to limit the volume of content here (also for fear of embarrassment), so haven't posted the entire code ie the handlechange, and next/previous step functions etc. but let me know if you need more info to answer this question better.试图限制这里的内容量(也是因为害怕尴尬),所以没有发布整个代码,即 handlechange 和下一步/上一步功能等,但如果您需要更多信息来更好地回答这个问题,请告诉我.

您需要将“数字”道具传递给 Step2 组件。

<Step2 currentStep={this.state.currentStep} number={this.state.number} />;

In you Step2 component you are not passing a prop called number instead you are passing a prop called currentStep .在您的Step2组件中,您没有传递一个名为number的道具,而是传递了一个名为currentStep的道具。 Try passing the number to the component like this尝试将数字传递给这样的组件

<Step2 currentStep={this.state.currentStep} number={this.state.number} />;

What is the purpose of the function函数的目的是什么

  get Number() {
    let result = this.number;
    console.log(result);
    return result
  }

you don't need to have something like this instead you can access the number as this.state.number .你不需要有这样的东西,你可以访问号码作为this.state.number and if you want to change it create a function like this如果你想改变它,创建一个这样的函数

changeNumber=(number)=> this.setState({number:number});

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

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