admin管理员组文章数量:1356294
my route looks like this:
<Route path="game/:room_id" element={
<GameProvider>
<Game />
</GameProvider>
} />
Game.jsx(partial) looks like this:
const Game = () => {
useHoo()
const {
currentPlayer,
players,
guessedPlayersId,
roundInfo,
messages,
timer,
joinedRoom,
socket,
socketReady,
room_id
}=useGameContext();
useEffect(()=>{
console.log('hello from game')
},[])
In my GameProvider.jsx i have created a useEffect hook with empty dependency with console.log('hello from provider')
and same in game.jsx console.log('hello from game');
and in useHoo hook i have also defined useEffect which console.log('hello from hoo');
Now here in Game.jsx i have called useHoo first then useGameContext which calls useContext(gameContext); and when i run it i get console.log output in this order:
1)hello from useHoo
2)hello from game
3)hello from provider
I am in confusion why hello from provider is printing at last when it is called before game's useEffect which should technically register that hook first.But it isn't
my route looks like this:
<Route path="game/:room_id" element={
<GameProvider>
<Game />
</GameProvider>
} />
Game.jsx(partial) looks like this:
const Game = () => {
useHoo()
const {
currentPlayer,
players,
guessedPlayersId,
roundInfo,
messages,
timer,
joinedRoom,
socket,
socketReady,
room_id
}=useGameContext();
useEffect(()=>{
console.log('hello from game')
},[])
In my GameProvider.jsx i have created a useEffect hook with empty dependency with console.log('hello from provider')
and same in game.jsx console.log('hello from game');
and in useHoo hook i have also defined useEffect which console.log('hello from hoo');
Now here in Game.jsx i have called useHoo first then useGameContext which calls useContext(gameContext); and when i run it i get console.log output in this order:
1)hello from useHoo
2)hello from game
3)hello from provider
I am in confusion why hello from provider is printing at last when it is called before game's useEffect which should technically register that hook first.But it isn't
Share Improve this question edited Mar 28 at 9:25 Steven 2,1213 gold badges23 silver badges35 bronze badges asked Mar 28 at 9:21 Call_Me_RhinoCall_Me_Rhino 71 silver badge1 bronze badge1 Answer
Reset to default 1It makes sense why hello from useHoo
runs before hello from game
- they're run in sequence. But why is hello from provider
after both, considering it's higher up in the tree?
Consider that when you write JSX, it gets parsed into React.createElement
calls. So
<GameProvider>
<Game />
</GameProvider>
actually gets parsed into something like:
React.createElement(
GameProvider,
{},
React.createElement(Game, {})
)
so it makes sense why the child has to run first - the function that renders the parent needs the results from the rendering of the child to run, similarly to something like this:
const useHoo = () => {
console.log('hello from hoo')
}
const Game = () => {
useHoo()
console.log('hello from game')
}
const GameProvider = (child) => {
console.log('hello from provider')
return child
}
GameProvider(Game())
本文标签: reactjsWhy is useEffect running in unexpected mannerStack Overflow
版权声明:本文标题:reactjs - Why is useEffect running in unexpected manner? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046449a2581613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论