-
Notifications
You must be signed in to change notification settings - Fork 558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Hooks] An official global state-management solution (a la redux
)
#90
Comments
Here is a basic example of what using an import React from 'react';
import ReactDOM from 'react-dom';
import {Atom, useAtom, swap} from '@dbeining/react-atom';
const stateAtom = Atom.of({
text: '',
data: {
// ...just imagine
},
});
const updateText = (evt) =>
swap(stateAtom, (state) => ({...state, text: evt.target.value}));
const loadSomething = () =>
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then((res) => res.json())
.then((data) => swap(stateAtom, (state) => ({...state, data})))
.catch(console.error);
const SomeComponent = () => {
const {count, data, text} = useAtom(stateAtom);
return (
<div>
<p>Text: {text}</p>
<button onClick={loadSomething}>
Load Data
</button>
<input
type="text"
onChange={updateText}
value={text} />
<p>{JSON.stringify(data, null, ' ')}</p>
</div>
);
};
const App = () => (
<div>
<SomeComponent /> // these components are kept in sync by useAtom
<SomeComponent /> // these components are kept in sync by useAtom
</div>
)
ReactDOM.render(<App />, document.getElementById('root')); |
Doesn't this proposal provide a solution? #89 |
@mfrachet , yeah that's what @gaearon pointed out to me. #89 wasn't posted until the day after I finished prototyping
@acdlite , if you happen to see this, I'd be super grateful to know what you think about |
Hi, thanks for your suggestion. RFCs should be submitted as pull requests, not issues. I will close this issue but feel free to resubmit in the PR format. |
Related to #68
I'm unsure if this is the appropriate place for this. If not, feel free to close.
The
useState
hook feels very similar to the way atoms work in reagent.cljs, except for one important part: inreagent
, multiple components can share oneatom
so that all of them re-render when it changes, whereasuseState
only creates state local to the component that called it and only that component (and its children) re-render when that state changes. To say it another way, React hooks offer little to no affordance for doing scalable global state management in the fashion ofredux
ormobx
, whereasreagent
atom
s do. However, I think it would be relatively simple to implementatom
s for React with a light abstraction on top ofuseState
anduseMutationEffect
.I've already implemented a minimal version of
Atom
forreact
, and so far I really like the result. I'm curious to know what others think.I'm just calling it react-atom for now and have published it (with TypeScript typings) to
npm
. Here are the docs. And here is a JS CodeSandbox and TS CodeSandbox to try it out live. If you're wondering, the API for it is mostly just plagiarized fromclojure/script
atoms.I think there would be a lot of benefit to
react.js
providing something like this as an official way for doing global state management. Doing so would really help unify the community and library ecosystem, and it would allow some performance optimizations to be managed internally byreact
. It could be made more plug-able too, so as to enable the various niceties made possible byredux
middleware.The new Hooks proposal seems like the perfect opportunity to introduce something like this. What do you think?
The text was updated successfully, but these errors were encountered: