title: How to manage a multi-step form with React?
body: <p>Here is the code of my multistep form:</p>
  
  <pre><code>import clsx from 'clsx';
  import React from 'react';
  import PropTypes from 'prop-types';
  import { makeStyles, withStyles } from '@material-ui/styles';
  import Step from '@material-ui/core/Step';
  import Stepper from '@material-ui/core/Stepper';
  import StepLabel from '@material-ui/core/StepLabel';
  import StepConnector from '@material-ui/core/StepConnector';
  import { Container, Row, Col, Button } from 'react-bootstrap';
  
  import Description from '@material-ui/icons/Description';
  import AccountCircle from '@material-ui/icons/AccountCircle';
  import DirectionsCar from '@material-ui/icons/DirectionsCar';
  
  import Step1 from '../components/Step1';
  import Step2 from '../components/Step2';
  import Step3 from '../components/Step3';
  
  const styles = () =&gt; ({
    root: {
      width: '90%',
    },
    button: {
      marginRight: '0 auto',
    },
    instructions: {
      marginTop: '0 auto',
      marginBottom: '0 auto',
    },
  });
  
  const ColorlibConnector = withStyles({ 
    alternativeLabel: {
      top: 22,
    },
    active: {
      '&amp; $line': {
        backgroundColor: '#00b0ff',
      },
    },
    completed: {
      '&amp; $line': {
        backgroundColor: '#00b0ff',
      },
    },
    line: {
      height: 3,
      border: 0,
      backgroundColor: '#eaeaf0',
      borderRadius: 1,
    },
  })(StepConnector);
  
  const useColorlibStepIconStyles = makeStyles({
    root: {
      backgroundColor: '#ccc',
      zIndex: 1,
      color: '#fff',
      width: 50,
      height: 50,
      display: 'flex',
      borderRadius: '50%',
      justifyContent: 'center',
      alignItems: 'center',
    },
    active: {
      backgroundColor: '#00b0ff',
      boxShadow: '0 4px 10px 0 rgba(0,0,0,.25)',
    },
    completed: {
      backgroundColor: '#00b0ff',
    },
  });
  
  function ColorlibStepIcon(props) {
    const classes = useColorlibStepIconStyles();
    const { active, completed } = props;
  
    const icons = {
      1: &lt;AccountCircle /&gt;,
      2: &lt;DirectionsCar /&gt;,
      3: &lt;Description /&gt;,
    };
  
    return (
      &lt;div
        className={clsx(classes.root, {
          [classes.active]: active,
          [classes.completed]: completed,
        })}
      &gt;
        {icons[String(props.icon)]}
      &lt;/div&gt;
    );
  }
  
  function getSteps() {
    return ['Dati Assicurato', 'Dati Veicolo', 'Dati Assicurazione'];
  }
  
  function getStepContent(step) {
    switch (step) {
      case 0:
        return &lt;Step1/&gt;;
      case 1:
        return &lt;Step2/&gt;;
      case 2:
        return &lt;Step3/&gt;;;
      default:
        return 'Unknown step';
    }
  }
  
  class HorizontalLinearStepper extends React.Component {
  
    constructor(props) {
      super(props);
      this.state = {
        activeStep: 0,
        agencyData: {}
      };
    }
  
    static propTypes = {
      classes: PropTypes.object,
    };
  
    isStepOptional = step =&gt; {
      return step === 1;
    };
  
    handleNext = () =&gt; {
      const { activeStep } = this.state;
      this.setState({
        activeStep: activeStep + 1,
      });
    };
  
    handleBack = () =&gt; {
      const { activeStep } = this.state;
      this.setState({
        activeStep: activeStep - 1,
      });
    };
  
    handleReset = () =&gt; {
      this.setState({
        activeStep: 0,
      });
    };
  
    logout = () =&gt; {
      localStorage.clear();
      this.props.history.push('/');
    }
  
    render() {
      const { classes } = this.props;
      const steps = getSteps();
      const { activeStep } = this.state;
  
      return (
        &lt;Container fluid&gt;
          &lt;div className={classes.root}&gt;
            &lt;Stepper alternativeLabel activeStep={activeStep} connector={&lt;ColorlibConnector /&gt;}&gt;
              {steps.map((label, index) =&gt; {
                const props = {};
  
                return (
                  &lt;Step key={label} {...props}&gt;
                    &lt;StepLabel StepIconComponent={ColorlibStepIcon}&gt;{label}&lt;/StepLabel&gt;
                  &lt;/Step&gt;
                );
              })}
            &lt;/Stepper&gt;
  
            &lt;div&gt;
              {activeStep === steps.length ? (
                &lt;div style={{textAlign: 'center'}}&gt;
                  &lt;h1 style={{textAlign: 'center', paddingTop: 100, color: '#7fc297'}}&gt;
                    TERMINATO
                  &lt;/h1&gt;
                  &lt;h4 style={{textAlign: 'center', paddingTop: 50}}&gt;
                    Tutti gli step sono stati completati con successo! 
                  &lt;/h4&gt;  
                  &lt;h4 style={{textAlign: 'center'}}&gt;  
                    Procedi con la generazione del QR Code.
                  &lt;/h4&gt;
                  &lt;Row style={{marginTop: '40px'}} className='justify-content-center align-items-center text-center'&gt;
                    &lt;Col md={{span: 3}}&gt;
                      &lt;Button 
                        style={{borderRadius: 30, borderWidth: 0, height: 50, width: 150, backgroundColor: '#f32a19', borderColor: '#f32a19'}}
                        disabled={activeStep === 0} 
                        onClick={this.handleReset} 
                      &gt;
                        Annulla
                      &lt;/Button&gt;
                    &lt;/Col&gt;
                    &lt;Col md={{span: 3}}&gt;
                      &lt;Button
                          style={{borderRadius: 30, borderWidth: 0, height: 50, width: 150, backgroundColor: '#00b0ff'}}
                          onClick={() =&gt; console.log('Click')}
                        &gt;
                        Procedi
                      &lt;/Button&gt;
                    &lt;/Col&gt;
                  &lt;/Row&gt;
                &lt;/div&gt;
              ) : 
              (
                &lt;Container style={{}}&gt;
                  &lt;h2 className={classes.instructions}&gt;{getStepContent(activeStep)}&lt;/h2&gt;
                  &lt;Row className='justify-content-center align-items-center text-center'&gt;
                    &lt;Col md={{span: 3}}&gt;
                      &lt;Button 
                        style={{marginTop: 10, backgroundColor: 'gold', borderRadius: 30, borderWidth: 0, height: 50, width: 150}}
                        disabled={activeStep === 0} 
                        onClick={this.handleBack} 
                      &gt;
                        Indietro
                      &lt;/Button&gt;
                    &lt;/Col&gt;
                    &lt;Col md={{span: 3}}&gt;
                      {
                        activeStep === steps.length - 1 ?
                        &lt;Button
                          style={{marginTop: 10, borderRadius: 30, borderWidth: 0, height: 50, width: 150, backgroundColor: '#7fc297'}}
                          onClick={this.handleNext}
                        &gt;
                        Finito
                        &lt;/Button&gt;
                        :
                        &lt;Button
                          style={{marginTop: 10, backgroundColor: '#00b0ff', borderRadius: 30, borderWidth: 0, height: 50, width: 150}}
                          onClick={this.handleNext}
                        &gt;
                        Avanti
                        &lt;/Button&gt;
                      }
                    &lt;/Col&gt;
                  &lt;/Row&gt;
                &lt;/Container&gt;
              )}
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/Container&gt;
      );
    }
  }
  
  export default withStyles(styles)(HorizontalLinearStepper);
  </code></pre>
  
  <p>It is composed of three steps and at each step I ask for many data. </p>
  
  <p>This is the code of one of the <code>Step</code> (they are all the same, the difference are the contents of the input fields):</p>
  
  <pre><code>import React from 'react';
  import { Container, Row, Col, Form } from 'react-bootstrap';
  
  export default function Step2(props) {
  
    return(
      &lt;Container&gt;
        &lt;Row style={{marginTop: '30px'}} className='h-100 justify-content-center align-items-center'&gt;
          &lt;Col md={{ span: 6 }} className='text-center my-auto'&gt;
            &lt;h3 style={{marginBottom: '1rem'}}&gt;Dati Veicolo&lt;/h3&gt;
            &lt;Form&gt;
              &lt;Form.Row&gt;
                &lt;Form.Group as={Col}&gt;
                  &lt;Form.Control
                    type='text' 
                    placeholder='Marca' 
                    required
                  /&gt;
                &lt;/Form.Group&gt;
                &lt;Form.Group as={Col}&gt;
                  &lt;Form.Control
                    type='text' 
                    placeholder='Targa' 
                    required
                  /&gt;
                &lt;/Form.Group&gt;
              &lt;/Form.Row&gt;
              &lt;Form.Group&gt;
                &lt;Form.Control
                  type='text' 
                  placeholder='Paese immatricolazione' 
                  required
                /&gt;
              &lt;/Form.Group&gt;
              &lt;h6 style={{marginBottom: '1rem'}}&gt;Possiede un rimorchio?&lt;/h6&gt;              
              &lt;Form.Group&gt;
                &lt;Form.Control
                  type='text' 
                  placeholder='Targa'
                /&gt;
              &lt;/Form.Group&gt;
              &lt;Form.Group&gt;
                &lt;Form.Control
                  type='text' 
                  placeholder='Paese immatricolazione'
                /&gt;
              &lt;/Form.Group&gt;
            &lt;/Form&gt;
          &lt;/Col&gt;
        &lt;/Row&gt;
      &lt;/Container&gt;
    );
  }
  </code></pre>
  
  <p>What I need to do is to check for errors at each step before the users pass to the following one, so that they can start fulfilling the second step of the form only in the case they have completed the first step correctly, and so on ...
  How can I do this check step by step?</p>
  
  <p>Moreover how can I collect all the data that I ask for in the main component of the form so that I can work with all those data after the users have finished fulfilling the whole form?</p>
  
  <p>At this <a href="https://codesandbox.io/s/sleepy-silence-w2n2z?fontsize=14&amp;hidenavigation=1&amp;theme=dark" rel="noreferrer">link</a> there is the example </p>