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

How is useRef being used in the react-typescript-apollo tutorial? #516

Open
saurabsalhotra opened this issue Oct 24, 2021 · 0 comments
Open

Comments

@saurabsalhotra
Copy link

saurabsalhotra commented Oct 24, 2021

I'm a bit confused as to how is useRef being used here. I'm sorry if this is not the place to ask doubts.

the useEffect at line 119 is is reassigning the ref to the latest incoming todo. Which it ideally shouldn't do as newestTodoId represent the newest todo that is currently visible. If you were to remove this line the app still works fine, which makes me question what does this line actually do.

Secondly even though newestTodoId is being assigned here a new value, if I do a console.log outside the useEffect, it still shows me the value of the topmost visible todo rather than the latest incoming todo.

https://github.com/hasura/learn-graphql/blob/master/tutorials/frontend/typescript-react-apollo/app-final/src/components/Todo/TodoPublicList.tsx

useEffect( () => { if (props.latestTodo && props.latestTodo.id! > newestTodoId.current) { setNewTodosCount(n => n + 1); **newestTodoId.current = props.latestTodo.id!;** } }, [props.latestTodo] );

can anybody please explain ? Removing this line doesn't affect the app at all.

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

No branches or pull requests

1 participant