jamiebuilds/unstated là dự án Frontend trên GitHub, viết chủ yếu bằng JavaScript, với 7.7k sao. State so simple, it goes without saying
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
"Unstated is a breath of fresh air for state management. I rewrote my whole app to use it yesterday."
Sindre Sorhus
"When people say you don't need Redux most of the time, they actually mean you do need Unstated. It's like setState on fucking horse steroids"
Ken Wheeler (obviously)
Guide
If you're like me, you're sick of all the ceremony around state management in
React, you want something that fits in well with the React way of thinking,
but doesn't command some crazy architecture and methodology.
So first off: Component state is nice! It makes sense and people can pick it
up quickly:
As a new React developer you might not know exactly how everything works, but
you can get a general sense pretty quickly.
The only problem here is that we can't easily share this state with other
components in our tree. Which is intentional! React components are designed to
be very self-contained.
What would be great is if we could replicate the nice parts of React's
component state API while sharing it across multiple components.
But how do we share values between components in React? Through "context".
Note: The following is part of the new React.createContext API
described in this RFC.
Behind the scenes our Containers are also event emitters that our app can
subscribe to for updates. When you call setState it triggers components to
re-render, be careful not to mutate this.state directly or your components
won't re-render.
setState()
setState() in Container mimics React's setState() method as closely as
possible.
If we want to test the relationship between our container and the component
we can again construct our own instance and inject it into the tree.
test('counter', async () => {
let counter = new CounterContainer();
let tree = render(
<Provider inject={[counter]}>
<Counter />
</Provider>
);
await click(tree, '#increment');
assert(counter.state.count === 1);
await click(tree, '#decrement');
assert(counter.state.count === 0);
});
Dependency injection is useful in many ways. Like if we wanted to stub out a
method in our state container we can do that painlessly.
test('counter', async () => {
let counter = new CounterContainer();
let inc = stub(counter, 'increment');
let dec = stub(counter, 'decrement');
let tree = render(
<Provider inject={[counter]}>
<Counter />
</Provider>
);
await click(tree, '#increment');
assert(inc.calls.length === 1);
assert(dec.calls.length === 0);
});
We don't even have to do anything to clean up after ourselves because we just
throw everything out afterwards.
FAQ
What state should I put into Unstated?
The React community has focused a lot on trying to put all their state in one
place. You could keep doing that with Unstated, but I wouldn't recommend it.
I would recommend a multi-part solution.
First, use local component state as much as you possibly can. That counter
example from above never should have been refactored away from component
state, it was fine before Unstated.
Second, use libraries to abstract away the bits of state that you'll repeat
over and over.
Like if form state has you down, you might want to use a library like
Final Form.
If fetching data is getting to be too much, maybe try out Apollo.
Or even something uncool but familiar and reliable like Backbone models and collections.
What? Are you too cool to use an old framework?
Third, a lot of shared state between components is localized to a few
components in the tree.
For this, I recommend using React's built-in React.createContext() API
and being careful in designing the API for the base components you create.
Note: If you're on an old version of React and want to use the new
context API, I've got you
Finally, (and only after other things are exhausted), if you really need
some global state to be shared throughout your app, you can use Unstated.
I know all of this might sound somehow more complicated, but it's a
matter of using the right tool for the job and not forcing a single
paradigm on the entire universe.
Unstated isn't ambitious, use it as you need it, it's nice and small for
that reason. Don't think of it as a "Redux killer". Don't go trying to
build complex tools on top of it. Don't reinvent the wheel. Just try it
out and see how you like it.
Passing your own instances directly to <Subscribe to>
If you want to use your own instance of a container directly to <Subscribe>
and you don't care about dependency injection, you can do so:
let counter = new CounterContainer();
function Counter() {
return (
<Subscribe to={[counter]}>
{counter => <div>...</div>}
</Subscribe>
);
}
You just need to keep a couple things in mind:
You are opting out of dependency injection, you won't be able to
<Provider inject> another instance in your tests.
Your instance will be local to whatever <Subscribe>'s you pass it to, you
will end up with multiple instances of your container if you don't pass the
same reference in everywhere.
Also remember that it is okay to use <Provider inject> in your application
code, you can pass your instance in there. It's probably better to do that in
most scenarios anyways (cause then you get dependency injection and all that
good stuff).
How can I pass in options to my container?
A good pattern for doing this might be to add a constructor to your container
which accepts props sorta like React components. Then create your own
instance of your container and pass it into <Provider inject>.
jamiebuilds/unstated thuộc nhóm Frontend trên TopGit, cùng 3 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về jamiebuilds/unstated ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/jamiebuilds/unstated là nguồn chính thức.
jamiebuilds/unstated có phải mã nguồn mở không?
Có — jamiebuilds/unstated phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/jamiebuilds/unstated.
jamiebuilds/unstated là gì?
jamiebuilds/unstated (jamiebuilds/unstated) là dự án JavaScript trên GitHub. Theo mô tả gốc: State so simple, it goes without saying
jamiebuilds/unstated so với các dự án Frontend khác thế nào?
jamiebuilds/unstated được TopGit xếp vào nhóm Frontend, với 7.7k sao GitHub và viết bằng JavaScript. Xem trang chủ đề Frontend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
Vì sao jamiebuilds/unstated được xếp vào nhóm Frontend?
TopGit xếp jamiebuilds/unstated vào nhóm Frontend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "management", "react", "state"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về unstated?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về unstated.