Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

Validator and state in React. #19

Open
charlo-chopin opened this issue Oct 14, 2020 · 2 comments
Open

Validator and state in React. #19

charlo-chopin opened this issue Oct 14, 2020 · 2 comments

Comments

@charlo-chopin
Copy link

Hi, I implemented step validator returning a hook state. But when the function is called, triggered by the "next" button, the state used in the validator is an old one, corresponding to the state during Its declaration in useEffect rule.

It seems the validation implementation doesn't support the changes state related to a hook, and only allows Its scope logic.

Do you know how I can use validator to return a boolean depending of state ? For instance return a variable passed by child component.

Thanks for help and don't hesitate if you need some payloads.

@r-bt
Copy link
Contributor

r-bt commented Dec 16, 2020

I've experienced the same issue, what solved it for me was using useRef for example

const [isValid, setIsValid] = useState(false)
const curIsValid = useRef(isValid);
curIsValid.current = isValid;

const steps = [
   ...
   {
      label: 'Test',
      validator: () => curIsValid.current,
   }
]

@fernando-pineda
Copy link

I've experienced the same issue, what solved it for me was using useRef for example

const [isValid, setIsValid] = useState(false)
const curIsValid = useRef(isValid);
curIsValid.current = isValid;

const steps = [
   ...
   {
      label: 'Test',
      validator: () => curIsValid.current,
   }
]

Thank you. I've been working with this library and spent several hours looking for an answer.

r-bt added a commit to r-bt/react-stepz that referenced this issue May 7, 2021
Fixes saini-g#19

We no longer use useReducer as it would create a closure around the state in the validator and hence it would not be updated. Instead we use contexts and a hook useProgressStep which never puts the validator function into state
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants