N
N
Nikita Shchypylov2018-01-16 13:11:52
React
Nikita Shchypylov, 2018-01-16 13:11:52

How to show items one by one in React?

Hello everyone
It turns out, I need the following logic:
When I added a new element to the base, I hide <div/>with this element and generate (or show a ready one? ). How can this be implemented?
Example:
I went to the site, I have a form in front of me: enter the first and last name. Entered, the form disappeared, another one opened, where you need to enter the date of birth and place of residence.
Need something like this
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artur Bordenyuk, 2018-01-16
@HighQuality

Make two shapes. Listen to submit. By submitting the first form - change the state - hide the first form and show the second form.
Everything is solved by regular means of react.

A
Anton Spirin, 2018-01-16
@rockon404

It could be something like this:

render() {
  const isUserInfoFormActive = ...; // your conditions
  const isUserAddressFormActive = ...; // your conditions

  return (
    <Wrapper>
      {isUserInfoFormActive && <UserInfoForm />}
      {isUserAddressFormActive && <UserAddressForm />}
    </Wrapper>
  );
}

If there are many steps, it is more rational to write a FormWidget:
class FormWidget extends Component {
  ...
  getContent() {
     const { children, activeStep } = this.props;

     return React.Children.map(children, (child, i) => {
      if (activeStep === i + 1) {
        return React.cloneElement(child, { key: i });
      }

      return null;
    });
  }

  handleNext = () => {
    ...
  };

  handlePrev = () => {
     ...
  };

  render() {
    ...
    return(  
      <div>
        <Content>{this.getContent()}</Content>
        <div>
           <Button onClick={this.handlePrev}>{prevButtonLabel}</Button>
           <Button onClick={this.handleNext}>{nextButtonLabel}</Button>
        </div>
      </div>
    );
  }
}

and betray the children all the steps:
render() {
  return (
     <FormWidget>
      <Step1 />
      <Step2 />
      <Step3 />
      <Step4 />
      <Step5 />
      <Step6 />
      <Step7 />
      <Step8 />
      <Step9 />
      <Step10 />
     </FormWidget>
  );
}

The example is very abstract. The idea is revealed.
All the same, writing the entire implementation takes a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question