Create maps-and-sets-usage.md (#997)

* Create maps-and-sets-usage.md

Hi, there. I was using this lib with React and I found that there is a specific way to use Maps and Sets, so I thought that a little update could help others. What do you guys think?

* Update docs/maps-and-sets-usage.md

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

* Update maps-and-sets-usage.md

The size went down cause using that way there's no going wrong...
Maybe it is still a good thing to have so others don't lose time like me??

* Update maps-and-sets-usage.md

* Update maps-and-sets-usage.md

Is it like this?

* run prettier

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
Co-authored-by: daishi <daishi@axlight.com>
This commit is contained in:
Gabriel Alves Cunha 2022-07-25 20:43:54 -03:00 committed by GitHub
parent 2517d5ab39
commit da15804ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,24 @@
## Map and Set Usage
You need to wrap Maps and Sets inside an object, and when you want it's update to be reflected (e.g. in React),
you do it by calling the setState on it:
**You can view a codesandbox here: https://codesandbox.io/s/late-https-bxz9qy**
```js
import create from 'zustand'
const useFooBar = create(() => ({ foo: new Map(), bar: new Set() }))
function doSomething() {
// doing something...
// If you want to update some React component that uses `useFooBar`, you have to call setState
// to let React know that an update happened.
// Following React's best practices, you should create a new Map/Set when updating them:
useFooBar.setState((prev) => ({
foo: new Map(prev.foo).set('newKey', 'newValue'),
bar: new Set(prev.bar).add('newKey'),
}))
}
```