I have a wizard component, that displays page with next and back buttons. Here is the sample:
function Wizard(props){
return (
<Grid container>
<Grid item xs={12}>
{RenderPage(props)}
</Grid>
<Grid item xs={12}>
<WizardButton title="Back" onClick={previousPage} />
<WizardButton title="Next" onClick={previousPage} />
</Grid>
</Grid>)
}
export function App(props) {
return (
<Wizard className='App'>
<Page>
<UserInfo />
</Page>
<Page>
<PaymentInfo />
</Page>
</Wizard>
);
}
the components <UserInfo/> and <PaymentInfo/> have their own forms implemented using formik. and these components are used in other parts of the application also.
my problem is that I want to perform validation of formik as soon as the next button is clicked. If the validation fails, then the wizard should stay on the current page. If there are no validation errors, then the wizard should continue.
so I need to notify the components to perform validation when next button is clicked. and I need to notify wizard about validation result to allow or disallow moving to next page.
As much as I understand this is reverse of how react works i.e. parent notifying child about events and child sending data to parent.
I'm trying a few ways. like for instance PubSub-js which is making code too much complex and also is not the react way. I'm also thinking that this surely is not the first time someone has encountered this problem but I'm unable to find out a way to solve this problem.

You can hold a state in your
Wizardcomponent to represent the validity. You can also pass a function to the child components to indicate how they can inform the parent about their form's validity status.I don't know how your
RenderPagefunction looks like but I swapped it with a render prop. You can adapt it to your needs.As for your child
Pagecomponents, you can now access these validity status related functions and pass them into the respective form components.Inside the form components whenever the form becomes valid, you can call
setValidand accordingly when it becomes invalid, you can callsetInvalid. Then the state in theWizardcomponent will change and theWizardButtoncomponent inside will be enabled/disabled.If you wish to pass the functions in another way, then Context is another option.