Чтобы решить, что вы делаете, вам нужно клонировать элемент и передать ему соответствующий реквизит. Вот пример в коде ручки, чтобы увидеть его вживую
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.Это довольно простой пример, поэтому, пожалуйста, потерпите меня.