I am trying to implement a specific Wizard
component which user can consume using the pattern below.
<Wizard {...wizardProps} onFinish={this.handleFinish}>
<WizardStep onValidate={() => this.componentARef.isValid()}>
<ComponentA onRef = { ref => (this.componentARef = ref)}/>
</WizardStep>
<WizardStep onValidate={() => this.componentBRef.isValid()}>
<ComponentB onRef = { ref => (this.componentBRef = ref)}/>
</WizardStep>
<WizardStep onValidate={() => this.componentCRef.isValid()}>
<ComponentC onRef = { ref => (this.componentCRef = ref)}/>
</WizardStep>
</Wizard>
Now considering the react way we can't/shouldn't call child component's method from parent component. Here I want to keep a isValid
method in each component which will be called from Parent Wizard
component on click of Next/Finish
button. React way suggest to move the state and logic to parent component. But that way I won't be able to reuse the same component e.g. ComponentA
in any other wizard or any other place or I will have to duplicate the validation logic in every parent component who is using ComponentA
. Using ref
or this approach I can easily access the child component's method(isValid
).
As of today(React version 16.6) I don't see any pitfalls using this pattern on need basis in react. What are the possible problem I may face using this pattern in react? And is there any better option in this particular example using which I can keep isValid
method in step component(e.g. ComponentA
) for reuse?
ComponentA
along with it's validation it seems to be pretty obvious and straight forward. I am just trying to understand does this termAntiPattern
comes with any disadvantages/pitfalls which I may face later. – ForbiddingWizardFooter
component. But I don't want to include it in my stepComponent(sayComponentA
) because then I won't be able to use that form/component at some other place where I probably need a different action e.g. formsubmit
button. – Forbidding