Api Suggestion #2311
Api Suggestion
#2311
-
What about the usage below. const store = create((set) => {
return {
count: 0,
increase(params: { by: number }) {
set((state) => {
return {
count: state.count + params.by,
};
});
},
decrease(params: { by: number }) {
set((state) => {
return {
count: state.count - params.by,
};
});
},
};
});
function Demo() {
const count = countStore.useStore((state) => state.count);
const onIncrease = () => {
countStore.increase({ by: 1 });
};
const onDecrease = () => {
countStore.decrease({ by: 1 });
};
return (
<div>
<div>{count}</div>
<div>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Answered by
dbritto-dev
Jan 26, 2024
Replies: 2 comments
-
@samonxian that's ok is called no store actions |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
dbritto-dev
-
You can do like this: const store = create((set) => {
return {
count: 0,
increase(params: { by: number }) {
set((state) => {
return {
count: state.count + params.by,
};
});
},
decrease(params: { by: number }) {
set((state) => {
return {
count: state.count - params.by,
};
});
},
};
});
function Demo() {
const count = countStore.useStore((state) => state.count);
const onIncrease = () => {
countStore.getState().increase({ by: 1 });
};
const onDecrease = () => {
countStore.getState().decrease({ by: 1 });
};
return (
<div>
<div>{count}</div>
<div>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</div>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@samonxian that's ok is called no store actions