kentcdodds / ama

Ask me anything!
https://github.com/kentcdodds/ama/issues?q=is%3Aissue+is%3Aclosed
685 stars 75 forks source link

How to test state value in useState() #804

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello Kent, hope you are doing well in these times, do you have an idea how to reach the value of state value like const [players, setPlayers] = useState(["Ali", "kent"]) is there a way to assert the value of (players) the initial value?

thaks

kentcdodds commented 4 years ago

Hi @Ali-Ghali,

You mean in testing? If that's what you're asking, them you don't want to do that. Read why here: https://kcd.im/imp-deets

ghost commented 4 years ago

hey, yes I mean in testing, I agree with you that it's redundant and I think it's not an issue to access the state in React-class based components, but still I need to access the state value in function-hooks components, which is tricky, any idea how to do that?

thanks

kentcdodds commented 4 years ago

No, I think you missed the point of the blog post. It shouldn't matter whether it's a class component or not. The internal state of your component should not be referenced in your test.