67
loading...
This website collects cookies to deliver better user experience
// @src/store.js
import create from "zustand";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
// Mutations will go here
}));
3000
you should have a visual result similar to this:addDrama()
that will receive a payload as a single argument.// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
// Logic goes here
});
})
),
// More mutations will go here
}));
.push()
method.// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
// More mutations will go here
}));
removeDrama()
. Which will also have the payload as its only argument.// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
// Logic goes here
})
),
// More mutations will go here
}));
// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
const dramaIndex = draft.kdramas.findIndex((el) => el.id === payload);
// More logic goes here
})
),
// More mutations will go here
}));
// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
const dramaIndex = draft.kdramas.findIndex((el) => el.id === payload);
draft.kdramas.splice(dramaIndex, 1);
})
),
// More mutations will go here
}));
patchDrama()
.// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
const dramaIndex = draft.kdramas.findIndex((el) => el.id === payload);
draft.kdramas.splice(dramaIndex, 1);
})
),
patchDrama: (payload) =>
set(
produce((draft) => {
// Logic goes here
})
),
}));
// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
const dramaIndex = draft.kdramas.findIndex((el) => el.id === payload);
draft.kdramas.splice(dramaIndex, 1);
})
),
patchDrama: (payload) =>
set(
produce((draft) => {
const drama = draft.kdramas.find((el) => el.id === payload.id);
// More logic goes here
})
),
}));
// @src/store.js
import create from "zustand";
import produce from "immer";
export const useStore = create((set) => ({
kdramas: [
{
id: Math.floor(Math.random() * 100),
name: "River Where the Moon Rises",
},
{
id: Math.floor(Math.random() * 100),
name: "The Crowned Clown",
},
],
addDrama: (payload) =>
set(
produce((draft) => {
draft.kdramas.push({
id: Math.floor(Math.random() * 100),
name: payload,
});
})
),
removeDrama: (payload) =>
set(
produce((draft) => {
const dramaIndex = draft.kdramas.findIndex((el) => el.id === payload);
draft.kdramas.splice(dramaIndex, 1);
})
),
patchDrama: (payload) =>
set(
produce((draft) => {
const drama = draft.kdramas.find((el) => el.id === payload.id);
drama.name = payload.name;
})
),
}));