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

[Basic] Hooks: useState type #403

Closed
priscilaandreani opened this issue Apr 15, 2021 · 1 comment
Closed

[Basic] Hooks: useState type #403

priscilaandreani opened this issue Apr 15, 2021 · 1 comment
Assignees
Labels
BASIC Basic Cheatsheet

Comments

@priscilaandreani
Copy link

What cheatsheet is this about? (if applicable)

Basic cheatsheet

What's your issue or idea?
Idea

instead of:

const [user, setUser] = React.useState<IUser | null>(null);

// later...
setUser(newUser);

you can use:

const [user, setUser] = React.useState<IUser>({} as IUser);

// later...
setUser(newUser);

(of course if IUser is an object)

its always great to start the state using its initial format

@priscilaandreani priscilaandreani added the BASIC Basic Cheatsheet label Apr 15, 2021
@priscilaandreani priscilaandreani changed the title [Basic] ISSUE_TITLE_HERE [Basic] Hooks: useState type Apr 15, 2021
@swyxio swyxio closed this as completed in 8a0ba06 Apr 16, 2021
@swyxio
Copy link
Collaborator

swyxio commented Apr 16, 2021

thanks @priscilaandreani, i've added this comment now! https://github.com/typescript-cheatsheets/react/blob/main/docs/basic/getting-started/hooks.md#usestate

happy to take more suggestions from you :)

stepanenko added a commit to stepanenko/react-ts-best-practices that referenced this issue May 3, 2021
* Updated README on 2021-04-02T14:21:23.928Z

* Fix missing parenthesis in troubleshooting types code example (typescript-cheatsheets#399)

Co-authored-by: swyx <shawnthe1@gmail.com>

* format

* minor improvements to "adding non-standard attributes" (typescript-cheatsheets#400)

* Added event types and their descriptions (typescript-cheatsheets#402)

* Added form types and their descriptions

I was looking for a complete list of form types and couldn't find any anywhere.
I went through the source code and extracted these with adding some description to them, either using MDN's description or some other website explanation.

* Apply suggestions from code review

Co-authored-by: swyx <shawnthe1@gmail.com>

* fix: Remove incorrect usage of "widening" (typescript-cheatsheets#405)

Using an intersection type here is actually narrowing the type, but the intent here seems to be to introduce type assertions, not talk about the difference between wider and narrower types.

* add type assertion for useState thanks @priscilaandreani

closes typescript-cheatsheets#403

* format

* Fixed typo (typescript-cheatsheets#406)

Fixed an issue where a paragraph described three different options as two.

* Add rollpkg as an option for creating libraries (typescript-cheatsheets#407)

* useLocalStorage Type detection bug (typescript-cheatsheets#409)

in my vs code it detects that the type of soredValue is (T | (value: T | ((val: T) => T)) => void)
but it must be just (T)
so it throws error 
with this change it solved.

* Updated README on 2021-04-25T20:13:23.562Z

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Denis LE <ledenis@users.noreply.github.com>
Co-authored-by: swyx <shawnthe1@gmail.com>
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
Co-authored-by: Lars <32460418+advename@users.noreply.github.com>
Co-authored-by: Gerrit Birkeland <gerrit@gerritbirkeland.com>
Co-authored-by: Christopher Kumm <40023417+ChrisCrossCrash@users.noreply.github.com>
Co-authored-by: Rafael Pedicini <rafael@rafgraph.dev>
Co-authored-by: Mandy YP <63463742+mmdHasan-yazdanPanah@users.noreply.github.com>
stepanenko added a commit to stepanenko/react-ts-best-practices that referenced this issue May 9, 2021
* Updated README on 2021-04-02T14:21:23.928Z

* Fix missing parenthesis in troubleshooting types code example (typescript-cheatsheets#399)

Co-authored-by: swyx <shawnthe1@gmail.com>

* format

* minor improvements to "adding non-standard attributes" (typescript-cheatsheets#400)

* Added event types and their descriptions (typescript-cheatsheets#402)

* Added form types and their descriptions

I was looking for a complete list of form types and couldn't find any anywhere.
I went through the source code and extracted these with adding some description to them, either using MDN's description or some other website explanation.

* Apply suggestions from code review

Co-authored-by: swyx <shawnthe1@gmail.com>

* fix: Remove incorrect usage of "widening" (typescript-cheatsheets#405)

Using an intersection type here is actually narrowing the type, but the intent here seems to be to introduce type assertions, not talk about the difference between wider and narrower types.

* add type assertion for useState thanks @priscilaandreani

closes typescript-cheatsheets#403

* format

* Fixed typo (typescript-cheatsheets#406)

Fixed an issue where a paragraph described three different options as two.

* Add rollpkg as an option for creating libraries (typescript-cheatsheets#407)

* useLocalStorage Type detection bug (typescript-cheatsheets#409)

in my vs code it detects that the type of soredValue is (T | (value: T | ((val: T) => T)) => void)
but it must be just (T)
so it throws error 
with this change it solved.

* Updated README on 2021-04-25T20:13:23.562Z

* Clarify useRef hook usages (typescript-cheatsheets#412)

Co-authored-by: swyx <shawnthe1@gmail.com>

* Update index.md

* Update from-flow.md

* docs: throw when ref.current is null (typescript-cheatsheets#413)

* Update types.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Denis LE <ledenis@users.noreply.github.com>
Co-authored-by: swyx <shawnthe1@gmail.com>
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
Co-authored-by: Lars <32460418+advename@users.noreply.github.com>
Co-authored-by: Gerrit Birkeland <gerrit@gerritbirkeland.com>
Co-authored-by: Christopher Kumm <40023417+ChrisCrossCrash@users.noreply.github.com>
Co-authored-by: Rafael Pedicini <rafael@rafgraph.dev>
Co-authored-by: Mandy YP <63463742+mmdHasan-yazdanPanah@users.noreply.github.com>
Co-authored-by: Thien Do <hello@thien.do>
Co-authored-by: Christina Grannas <christina.grannas@gmail.com>
chef1119 pushed a commit to chef1119/chef-typescript that referenced this issue Oct 20, 2021
bernssolg added a commit to bernssolg/My-React-Sample that referenced this issue Feb 28, 2022
erinodev added a commit to erinodev/My-React-project that referenced this issue Feb 28, 2022
kevindavies8 added a commit to kevindavies8/react-full-stack-developer that referenced this issue Aug 24, 2022
johnfrench3 pushed a commit to johnfrench3/react-Fronted-developer that referenced this issue Sep 7, 2022
ericbrown2716 added a commit to ericbrown2716/react-stack-build-website that referenced this issue Sep 29, 2022
peterjohnson4987 added a commit to peterjohnson4987/full-stack-developer-react that referenced this issue Oct 3, 2022
renawolford6 pushed a commit to renawolford6/react-husky-website that referenced this issue Oct 6, 2022
renawolford6 added a commit to renawolford6/react-dev-build-doc- that referenced this issue Nov 10, 2022
coopfeathy added a commit to coopfeathy/cheatsheet that referenced this issue Dec 4, 2022
dreamcoder75 added a commit to dreamcoder75/react-sample that referenced this issue Jan 15, 2023
holyblock pushed a commit to holyblock/chart that referenced this issue Feb 27, 2023
AIDevMonster added a commit to AIDevMonster/Awesome-React that referenced this issue Jun 21, 2023
whiteghostDev added a commit to whiteghostDev/Awesome-React that referenced this issue Aug 6, 2023
cedev935 added a commit to cedev935/React-TypeScript that referenced this issue Sep 11, 2023
aleksandaralek added a commit to aleksandaralek/typescript-react-cheatsheet that referenced this issue Oct 24, 2023
xbucks pushed a commit to xbucks/react-cheatsheets that referenced this issue Oct 24, 2023
KonohaBrain125 pushed a commit to KonohaBrain125/React-Typescript that referenced this issue Oct 26, 2023
TOP-10-DEV added a commit to TOP-10-DEV/typescript-cheatsheets-react that referenced this issue Dec 8, 2023
secretsuperstar1109 added a commit to secretsuperstar1109/react-typescript-cheatsheets that referenced this issue Dec 9, 2023
EugeneYoona added a commit to EugeneYoona/React_full_src that referenced this issue Apr 10, 2024
fairskyDev0201 added a commit to fairskyDev0201/typescript-cheatsheet that referenced this issue Apr 17, 2024
alisenola added a commit to alisenola/react-cheatsheets that referenced this issue May 29, 2024
marceloaguilera94 pushed a commit to marceloaguilera94/react that referenced this issue Jun 5, 2024
miracle-soft-expert added a commit to miracle-soft-expert/React-TypeScript that referenced this issue Oct 15, 2024
super-dev03 added a commit to super-dev03/react-test that referenced this issue Dec 25, 2024
Misha0421 added a commit to Misha0421/React-Project that referenced this issue Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BASIC Basic Cheatsheet
Projects
None yet
Development

No branches or pull requests

2 participants