Comments (6)
@drcmda if you use a callback, your component still rendering twice. Once for the initial value and then after you call the init, right?
And that would the the same as using useEffect
const { value, setValue } = useStore();
useEffect(() => {
setValue(initValue);
}, [initValue]);
Btw, cool game!
from zustand.
function A(props) {
const [[useStore]] = useState(() => create(set => ({ a: props.a }))
i usually use a callback though, an init() function that i call after components have been created. here's a recent example: https://codesandbox.io/s/react-three-fiber-untitled-game-i2160 (down there where the canvas component it, needed the store to be global, but relies on the camera, which is created later).
from zustand.
Look like resolved. Please file a new issue for further discussion.
from zustand.
yes, that would add one render.
from zustand.
creating the store inside the components state won't allow you to have an initial state. Once the component is unmounted the state is destroyed as does the store. So basically putting the store in state is the same as just using the state by itself
from zustand.
@joelmoss, you can do it in that way too
const [useStore, api] = create(set => ({
a: "dafaultState",
set: fn => set(produce(fn))
}));
const ExampleComponent = ({ a }) => {
const initialState = { a };
const ref = useRef(initialState);
const state = ref.current;
const store = useStore();
useEffect(
() =>
api.subscribe(
state => {
if (state) {
ref.current = state;
}
},
state => state
),
[]
);
return (
<div
onClick={() =>
store.set(state => {
state.a = "nextState";
})
}
>
Test: {state.a}
</div>
);
};
https://codesandbox.io/s/elastic-sutherland-r43x5
from zustand.
Related Issues (20)
- When the set operation is executed in the 「then callback」, the 「set operation」 takes nearly a hundred times longer.
- Resetting Multiple Stores Example Doesn't Work with the Currying Required Elsewhere for Typescript Support
- 123e
- In nextjs13's app directory, how can the server components send Fetch?
- Unnecessary Renders Triggered by a Hook after Migrating to Zustand
- with immer midderware, Array type wrong HOT 2
- The reactNode is stored in the store, sometimes causing the component to lose update. HOT 1
- Is there a way to reset data when switching routes? The previous method cannot take effect#1192
- Type problem with persist HOT 3
- Reset store not working on log out
- I can't update state with new data from Tanstack Query HOT 2
- The app hanging when initializing the store with and array of objects. HOT 4
- Is there any chance to simplify types?
- How to move the router state to store?
- Website graphics rendering issues (Linux) HOT 3
- shallow not support nested object HOT 1
- Are these two methods exactly the same? HOT 1
- It’s not recommended to suspend a render based on a store value returned by useSyncExternalStore.
- RFC: How is v5-alpha? HOT 11
- should useShallow add into the documents?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zustand.