Array with one less item breaks the store.
Ofer-Gal opened this issue · comments
What version of this package are you using? 0.5.0
What operating system, Node.js, and npm version? 14
What happened?
My Store has an array
export const { Store, useStore } = createStore({ upfiles: [], projectNumber: 'None' });
using:
const [upfiles, setUpfiles] = useStore.upfiles();
ina any component, I can add items to the upfiles array and make changes to the items in any component no problems.
But when I setUpfiles with a copy of upfiles with one less item, I get an error: "TypeError: n is not a function"
What did you expect to happen?
The upfiles array should now be one less item all over.
Are you willing to submit a pull request to fix this bug?
I would not know where to start 👎
How is your code to remove this item?
It should work with a filter:
setUpfiles(upfiles.filter(isItemToKeep))
I even tried setUpfiles([])
and it failed.
Must be something related to the SPFx workbench.
I will try to figure out a workaround
It was my problem. still a great library!
@Ofer-Gal Thanks. I'm working with 2 new features for the next release
Is it possible to have more than 1 store? if so, can you show a simple example.
Yes. You need to use the createStore
function to create all the stores you want:
Example:
store.js
export const { useStore: useCart } = createStore({ price: 0, items: [] })
export const { useStore: useCounter } = createStore({ count: 0 })
Cart.js
import { useCart } from './store'
export default function Cart() {
const [price, setPrice] = useCart.price()
// ... rest
}
Counter.js
import { useCounter } from './store'
export default function Counter() {
const [count, setCount] = useCount.count()
// ... rest
}
I thought you had to user "useStore" but if we can use "use{whatever}" and just stick to it, it is freaking awesome. Thank you