-
Describe the bugDocs says
But this code does not work. function Repro() {
const [state, setState] = createStore({ items: [{ id: Date.now() }] });
const addItem = () => setState("items", state.items.length, { id: Date.now() });
const deleteItemNG = (idx: number) => setState("items", idx, undefined!); // 👈🏻
const deleteItemOK = (idx: number) => setState("items", (items) => [...items.filter((_, i) => i !== idx)]);
return (
<ErrorBoundary fallback={(err) => <pre>{err.message}</pre>}>
<button onClick={addItem}>ADD</button>
<hr />
<For each={state.items}>
{(item, idx) => (
<div>
{item.id}
<button onClick={() => deleteItemNG(idx())}>DEL(NG)</button>
<button onClick={() => deleteItemOK(idx())}>DEL(OK)</button>
</div>
)}
</For>
</ErrorBoundary>
);
} It ends up with Am I missing something? Your Example Website or Apphttps://playground.solidjs.com/anonymous/7cafb3a2-7dc3-44f5-9cd2-75f35aef92ec Steps to Reproduce the Bug or Issue
Expected behaviorDeleting can be done by Screenshots or VideosNo response Platform
Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
You are using the api incorrectly. Try |
Beta Was this translation helpful? Give feedback.
-
Maybe no, I'd like to delete only single item in array. { items: [{ id: 1 }, { id: 2 }, { id: 3 }] }
// ↓
{ items: [{ id: 1 }, { id: 3 }] }
For my understanding, this means like, { items: [{ id: 1 }, { id: 2 }, { id: 3 }] }
// ↓
{ } Or does it mean setting |
Beta Was this translation helpful? Give feedback.
-
If you are going to remove an item, filter the item and set the resulting array as the new value. setState("items", arr.filter(el => el.id !== 1)) If you are going to update, you can map over the items and set the resulting array as the next value. Alternatively you can use path pattern: https://www.solidjs.com/docs/latest/api#updating-stores |
Beta Was this translation helpful? Give feedback.
-
The answer is above is correct.. deleting a key in an array leaves a hole. The actual operation is should be a filter, slice etc... I'm going to move this to discussions so we can track this as a Q&A. |
Beta Was this translation helpful? Give feedback.
If you are going to remove an item, filter the item and set the resulting array as the new value.
If you are going to update, you can map over the items and set the resulting array as the next value. Alternatively you can use path pattern: https://www.solidjs.com/docs/latest/api#updating-stores