简体   繁体   English

我对步骤计数器有问题-reactjs表单

[英]I have a issue with a counter of steps - reactjs form

I have a problem with the function that counts the steps of a form. 我的函数无法计算表格的步数。

this is undefined thisundefined

nextStep = () => {
  const { step } = this.step;
  this.setState({
    step: step + 1
  });
}
export class FormUserDetails extends Component {
  continue = e => {
    e.preventDefault();
    debugger;
    this.props.nextStep();
  };
  render() {
    const { values, handleChange } = this.props;
    return (
      <div class="page">
        <h2 class="box-title">Who are you ?</h2>
        <form onSubmit={this.continue}>
          <div class="content">
            <input
              class="form-input"
              type="text"
              placeholder="Surname"
              name="firstName"
              defaultValue={values.firstName}
              onChange={handleChange}
              required
            />
TypeError: Cannot read property 'step' of undefined
UserForm.nextStep
src/components/form/UserForm.js:12
   9 |   email: ""
  10 | };
  11 | //proceed to the next steep
> 12 | nextStep = () => {
     | ^  13 |   const { step } = this.step;
  14 |   this.setState({
  15 |     step: step + 1

Thanks you for you help ! 谢谢您的帮助!

If the step is in the state then it can be fetched like this: 如果该step处于state则可以这样获取它:

const { step } = this.state;
this.setState({
  step: step + 1
});

It'd be const { step } = this.state; 这将是const { step } = this.state;

Also if your new state depends on the value of the current state, use functional form of setState : 另外,如果您的新状态取决于当前状态的值,请使用setState函数形式:

  this.setState(state => ({
     ...state,
     step: state.step + 1
   }));

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

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