Копировать реквизит от родителя в ReactJs? - PullRequest
0 голосов
/ 08 июня 2018

Я использую реагирование 16.

Я создаю волшебника и пытаюсь создать его так, чтобы я мог использовать его в любом месте на своем сайте, где он мне нужен.

Вот мой код

export default class App extends Component {
  constructor(props) {
    super(props);
  }

  render() {

    const steps = [ {name: 'Step 1', component:   <Step1 /> }, 
                    {name: 'Step 2', component:   <Step2 /> }];
    return (
      <React.Fragment>
            <WizardComponent  steps={steps}/>
      </React.Fragment>
    );
  }
}



import React, { Component } from "react";
import ReactDOM from "react-dom";


import WizardStepCompoent from "./WizardStepCompoent";


export default class WizardComponent extends Component {
  constructor(props) {
    super();
    this.state = {
      currentStep: 1
    };
  }
  next(e) {
    // need to update the current step look 
    // const step =  this.props.steps.find(x => x.name == "Step 1")
    // step.changeToCompleted() ----> go form {name: "Step 1",  component: <Step 1/>} to {name: "Step 1",  component: <Step 1/>, isCompleted: true}
  }
  render() {
    const props = this.props;
    return (
      <div>
        <div className="steps">
          {props.steps.map(step => {
            return <WizardStepCompoent step={step} />;
          })}
        </div>
         {props.steps.map(step => {
            return step.component; // later on use current state to figure out which one to render ie if currentStep == 1 then only render the 1st component 
          })} 


        <button onClick={ (e) => this.next(e) }>Next</button>
      </div>
    );
  }
}


export default class WizardStepCompoent extends Component {
    render() {
      const props = this.props;
      const step = props.step;
      var completed = "";
      if(step.isCompleted)
      {
          completed = "step-item is-completed";
      } else {
            completed = "step-item";
      }
      return (
        <div className={completed}>      
          <div className="step-marker">
            <span className="icon">
              <i className="fa fa-check" />
            </span>
          </div>
          <div className="step-details">
            <p className="step-title">{step.name}</p>
            <p>This is the first step of the process.</p>
          </div>
        </div>
      );
    }

1 Ответ

0 голосов
/ 08 июня 2018

Чтобы решить, что вы делаете, вам нужно клонировать элемент и передать ему соответствующий реквизит. Вот пример в коде ручки, чтобы увидеть его вживую

class App extends React.Component {
  render() {
    const steps = [ {id: 'first', name: 'Step 1', component:   <Step1 /> }, 
                    {id: 'second', name: 'Step 2', component:   <Step2 /> }];
    return (
      <div>
            <WizardComponent  steps={steps}/>
      </div>
    );
  }
}

class WizardComponent extends React.Component {
  constructor(props) {
    super();
    this.state = {
      currentStep: 0,
      completedSteps: {
        'first': {completed: false},
        'second': {completed: false}
      }
    };
  }
  next(e) {
    const {currentStep} = this.state
    const completedSteps = {...this.state.completedSteps}
    const current = this.props.steps[currentStep]
    completedSteps[current.id].completed = true
    this.setState({currentStep: this.state.currentStep + 1, completedSteps})
  }
  render() {
    const props = this.props;
    const {currentStep, completedSteps} = this.state
    return (
      <div>
        <div className="steps">
          {props.steps.map(step => {
            return <WizardStepCompoent step={step} />;
          })}
        </div>
         {props.steps.map((step, i) => {
            return React.cloneElement(step.component, completedSteps[step.id]); // later on use current state to figure out which one to render ie if currentStep == 1 then only render the 1st component 
          })} 


        <button onClick={ (e) => this.next(e) }>Next</button>
      </div>
    );
  }
}

Мясо того, что происходит, находится в клоне, а также при нажатии кнопки «Далее».Я добавил к вашим шагам идентификатор как способ перекрестной ссылки на каждый шаг, затем вы просматриваете текущий шаг по его идентификатору и устанавливаете для завершенного значение true.Это довольно простой пример, поэтому, пожалуйста, потерпите меня.

...