DisLido
DisLido

Reputation: 45

React 19 `use` vs `useContext`

React 19 has a new API use that can read context. use seems to be able to do anything that useContext can do. The document says use is preferred over useContext because it is more flexible. Should I replace all useContext with use and forget useContext forever? Is useContext better than use in some case?

Upvotes: 0

Views: 87

Answers (1)

Chukwujiobi Canon
Chukwujiobi Canon

Reputation: 4091

If it ain’t broken, it don’t need fixing. Yes, use is more flexible than useContext. It can even be invoked in if conditionals and loops which useContext couldn’t. So from now onwards, use should be preferred to useContext for its flexibility. That doesn’t mean you should rewrite good ol’ working code.

The use API can even read a Promise and return the resolved value of the Promise whereas Promise rejection is thrown. You can deal with rejected Promises your way though. [react.dev]

You can stream data and a whole lot more so prefer it moving forward that’s all.

Upvotes: 0

Related Questions