Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Ridiculously simple state stores with performant retrieval anywhere in your React tree using the wonderful concept of React hooks!
<Suspense/>
to have complete control over their UI states!Originally inspired by the now seemingly abandoned library - bey. Although substantially different now- with Server-side rendering and Async Actions built in! Bey was in turn inspired by react-copy-write.
Try out a quick example:
This is taken directly from the documentation site, to give you a quick overview of Pullstate here on github. Be sure to check out the site to learn more.
To start off, install pullstate
.
yarn add pullstate
Define the first state store, by passing an initial state to new Store()
:
import { Store } from "pullstate";
export const UIStore = new Store({
isDarkMode: true,
});
Then, in React, we can start using the state of that store using a simple hook useStoreState()
:
import * as React from "react";
import { useStoreState } from "pullstate";
import { UIStore } from "./UIStore";
export const App = () => {
const isDarkMode = useStoreState(UIStore, s => s.isDarkMode);
return (
<div
style={{
background: isDarkMode ? "black" : "white",
color: isDarkMode ? "white" : "black",
}}>
<h1>Hello Pullstate</h1>
</div>
);
};
The second argument to useStoreState()
over here (s => s.isDarkMode
), is a selection function that ensures we select only the state that we actually need for this component. This is a big performance booster, as we only listen for changes (and if changed, re-render the component) on the exact returned values - in this case, simply the value of isDarkMode
.
Great, so we are able to pull our state from UIStore
into our App. Now lets add some basic interaction with a <button>
:
return (
<div
style={{
background: isDarkMode ? "black" : "white",
color: isDarkMode ? "white" : "black",
}}>
<h1>Hello Pullstate</h1>
<button
onClick={() =>
UIStore.update(s => {
s.isDarkMode = !isDarkMode;
})
}>
Toggle Dark Mode
</button>
</div>
);
Notice how we call update()
on UIStore
, inside which we directly mutate the store's state. This is all thanks to the power of immer
, which you can check out here.
Another pattern, which helps to illustrate this further, would be to actually define the action of toggling dark mode to a function on its own:
function toggleMode(s) {
s.isDarkMode = !s.isDarkMode;
}
// ...in our <button> code
<button onClick={() => UIStore.update(toggleMode)}>Toggle Dark Mode</button>
Basically, to update our app's state all we need to do is create a function (inline arrow function or regular) which takes the current store's state and mutates it to whatever we'd like the next state to be.
Something interesting to notice at this point is that we are just importing UIStore
directly and running update()
on it:
import { UIStore } from "./UIStore";
// ...in our <button> code
<button onClick={() => UIStore.update(toggleMode)}>Toggle Dark Mode</button>
And our components are being updated accordingly. We have freed our app's state from the confines of the component! This is one of the main advantages of Pullstate - allowing us to separate our state concerns from being locked in at the component level and manage things easily at a more global level from which our components listen and react (through our useStoreState()
hooks).
FAQs
Simple state stores using immer and React hooks
The npm package pullstate receives a total of 16,129 weekly downloads. As such, pullstate popularity was classified as popular.
We found that pullstate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.