I have an array of components which are all separate views that I am injecting to it's parent via a prop
const steps =
[ <StepOne wizardContext={shippingObj.from} onAction={this.handleFrom}/>,
<StepTwo wizardContext={shippingObj.to} onAction={this.handleFrom} />,
<StepThree wizardContext={shippingObj.weight} onAction={this.handleFrom} />,
<StepFour wizardContext={shippingObj.shippingOption} onAction={this.handleFrom} />,
<Confirm wizardContext={shippingObj} onAction={this.handleFrom} />
]
<Wizard steps={steps}/>
In the Wizard component I'm rendering each step based on the actual step
{this.props.steps[this.state.compState]}
Also in the wizard component I have a method
handleFrom(val) {
let state = val.stage,
key = Object.keys(val)[0];
this.setState({
wizardContext: { ...this.state.wizardContext, [key]: val[key]}
})
}
However onAction is undefined meaning it doesn't have access to "this"
If I include each one of them individually within parent wizard component I do have access to this and it works
/*THIS WORKS */
switch (this.state.compState) {
case 1:
return <StepOne onAction={this.handleFrom} data={this.state.wizardContext.from} />
case 2:
return <StepTwo onAction={this.handleFrom} data={this.state.wizardContext.to} />
case 3:
return <StepThree onAction={this.handleFrom} data={this.state.wizardContext} />
case 4:
return <StepFour onAction={this.handleFrom} data={this.state.wizardContext.shippingOption} />
case 5:
return <Confirm shippingLabel={this.state.wizardContext} labelkeys={this.shippingKeys} />
}
however I want to decouple the steps from the actual wizard
Is there a way to access this or parent methods from the child if the child is an array of components? Is there a cleaner way to do this? the components will render just don't have access to "this" in props Any constructive feedback is appreciated