Skip to content
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

Is it possible to set the current step to state? #282

Open
koiski opened this issue Aug 23, 2020 · 4 comments
Open

Is it possible to set the current step to state? #282

koiski opened this issue Aug 23, 2020 · 4 comments
Labels
v1 Issue related on previous version

Comments

@koiski
Copy link

koiski commented Aug 23, 2020

If I do something like this:
getCurrentStep={curr => setState({ currentStep: curr })}
I get an error saying: "Cannot update a component from inside the function body of a different component"

Is there any way of setting the current step number to a state variable?

@Dieter103
Copy link

I'm using it in a function component and this works for me
getCurrentStep={(curr) => { setCurrentStep(curr); }}

@smartmock
Copy link

Hi. I experienced that too. IMO the problem is that the getCurrentStep function is invoked, and the Tour component gets rerendered due to the state change (if the Tour component depends on any state-managed properties). In this case, you have a cyclic dependency. Updating state triggers getCurrentStep what triggers the state change etc.

@koiski
Copy link
Author

koiski commented Nov 8, 2020

A workaround for this is setting a timout function
setTimeout(() => { setState({ currentStep: curr }); }, 0);

You can see here, why this works: https://www.gitmemory.com/issue/Paratron/hookrouter/110/604592927

@yanickrochon
Copy link

yanickrochon commented Jan 26, 2022

When you have setState({ currentStep: curr }), you essentially create a brand new state each and every time. Therefore, you trigger a component refresh. In a nutshell, this is the expectation :

const stateA = { foo: 1 };
const stateB = { foo: 1 };

stateA === stateB;   // false, consider a state change
stateA.foo === stateB.foo;  // true, state is the same

In other words, to prevent triggering a state change, compare currentStep inside of your state change. For example :

getCurrentStep={curr => 
    setState(prevState => prevState.currentStep !== curr ? { currentStep:curr } : prevState)
}

Essentially : if prevState.currentStep is different from curr, then create a new state, otherwise keep the previous one. In other words, setState(prevState => prevState) will never trigger a component update because the state does not change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v1 Issue related on previous version
Projects
None yet
Development

No branches or pull requests

5 participants