M
M
Maxim Semiletkin2021-03-06 21:46:44
React
Maxim Semiletkin, 2021-03-06 21:46:44

What happens in useState?

Good day. I decided to dig into react to understand how everything works. Found this code,

export function useState"S"(initialState: (() => S) | S) {
const dispatcher = resolveDispatcher();
return dispatcher.useState(initialState);
}

which is used for the useState hook. It's not clear what the "S" is - there should be more and less signs - in front of the arguments and what kind of initialState construction: (() => S) | S. How it checks bit by bit and what kind of thing gets into the function if the first argument is executed. I can't find any info. Can you give some clear information about this? And better links to literature or articles that chew on these issues.
Thanks for the answer . All for a delicious virtual cookie

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WbICHA, 2021-03-06
@edding8750

https://www.typescriptlang.org/

A
Anvar Shakhmaev, 2021-03-06
@RxR

S is the type of the object passed to useState.
(() => S) | S - useState can be passed either a function that returns an object of type S, or an object of this type itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question