-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multistep Wizard example - How to access this.state.value from Wizard inside each Wizard.page? #844
Comments
@Huanzhang89 Have you considered abstracting the |
@njj I did consider that but the issue here is passing the props down to Wizard.Page and accessing them inside Wizard.Page. Even if we abstract the I have however found a solution to this using renderProps! The problem is that inside the static method Wizard.Page, the props are not passed down to the children as it simply returns the children.
By modifying this static method to
And also modifying the JSX inside each Wizard.Page to be a function that returns the JSX, we can pass the parentState down to each Wizard.Page, like so.
props in the above function = parentState Hope this helps anyone else getting stuck with this issue! |
@Huanzhang89 Nice, you could also do this by using { React.cloneElement(activePage, { ...props }) |
Oh really, do you mean instead of just {activePage} inside the render function of the Wizard? But if you look at the first post I made, I am already using React.cloneElement to pass the I think we still need to renderProps inside Wizard.Page to pass the |
@Huanzhang89 Right then you will need to handle it in the Page function again. Either way I think works. I ended up doing something similar, but I'm passing the Formik props (values, fns, etc..) so my fields can have them. |
Ah ok I see what you mean. Maybe we should create a PR to either update the current example or create a new multipage example? I can see a lot of use cases where we would need to access the form data inside each page and its not immediately clear how this can be done from the current example. |
@njj Actually after some testing I found that my method doesn't update the form values correctly as the |
Hi @Huanzhang89 @njj I am stuck on the same issue. Can you please share a code snippet or example to fix it. Any help would be really appreciate! |
@shoaibkhan94 here's the code example with their solution: |
@longnt80 Thanks. It's working! |
Sorry guys been away for a while, thanks @longnt80 for working it out and helping :) |
Hey I saw this issue. Im having a somewhat similar problem. Im trying to access the values from a radiobutton and use that as an conditional rendering of a
And I have 3 radiobuttons on the first page and I would like to use the value of the radiobuttons to conditionally render pages like:
But it is not working. Im getting a
I must be doing something wrong when trying to render base on the value of the radioGroup. Anyone tried anything like this? |
Hey Tim, can you log out what children is inside Can you also page your |
@Huanzhang89 heres the
|
Okay so one thing I notice is that you are spreading children and assigning it to the props of Formik. You should remove that. However the true culprit for your error is actually the fact that you spread props inside parentState. This means that your
|
@Huanzhang89 ah ok, so remove children from here: |
Yes you should remove For the second point I'll try to explain. The entire object of the second argument for React.cloneElement gets assigned to the props of the resulting React element. Remember the props of a React Element is simply an object, so in your case the props of your Element would be Did making that change fix your issue btw? |
@Huanzhang89 I think Im with you, the only thing is that |
Ah sorry I made a mistake with that, it should be But I would actually recommend not spreading |
Ah alright, ok great thanks. Well yes I had to do a quite verbose and ugly solution to my problem:
How do you mean? |
You can check out this wizard component built for formik: |
closing in favor of #1315 |
I wrote an example using Reactstrap (UI library) with Yup + Formik, demonstrating all the important features for a wizard: https://github.com/vincentntang/multistep-wizard-formik-yup-reactstrap |
I am using the multistep wizard from the examples section to implement a 2 page form but there is one small issue that I have and haven't been able to figure out.
The example showed how to easily hook up the input fields so that changes update automatically in the Wizard component's state.
For example I have a Field like the one below and I want the user's selection in this Field to affect the label of a sibling input. I am struggling to find a way to access the value of the Colour field.
I have successfully attached state of the Wizard component to each child component using the following code:
But what I am stuck on is how to access the props in Wizard.Page, since we are just calling the static method Page on the Wizard component and have no access to
this
.The text was updated successfully, but these errors were encountered: