ianstormtaylor/react-values — 1.0k★ trên GitHub (JavaScript). A set of tiny React components for handling state with render props.
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.
react-values gives you a set of simple, composable helpers that let you build more complex, stateful UI components like toggles, dropdowns, lists, checkbox groups, popovers, tooltips, you name it!
It does this using a small render-prop-based API that exposes helpful transforms like toggle, increment, filter, etc. depending on the type of value, all based on JavaScripts native value types...
Any values provide simple transforms like set and clear.
Array values provide native methods like push, pop, filter, etc.
Boolean values provide toggle, which we've all re-implemented 100 times.
Date values provide really helpful transforms like setHours and incrementMonth.
Map values provide native methods like set, delete and clear.
Number values provide increment and decrement, which have also been re-written in every codebase ever.
Object values provide helpful transforms like set, unset and assign.
Set values provide native methods like add, delete and clear.
String values provide native methods like concat, repeat, trim, etc.
This saves you from constantly re-writing the same state management logic, so you can keep your components focused on behavior and presentation.
For example, here's the classic state management "counter" example:
Of going further, here's a full fledged <Toggle> (respecting value/defaultValue and providing onChange) implemented in just a few lines of code using a <BooleanValue>:
But you can go further, because react-values can "connect" a single value across multiple components. This is helpful any time you need a "global" piece of state in your app, without wanting to add tons of complexity.
For example, using the <Toggle> from above, here's a modal you can open and close from anywhere in your app:
The primitives react-values gives you seem simple at first, but they can be composed together to create complex behaviors that are still easy to reason about, in just a few lines of code.
Why?
While building an app with React, you end up building a lot of stateful components in the process. Whether at the UI kit level for things like toggles, tooltips, checkbox groups, dropdown, etc. Or at the app level for modals, popovers, sorting, filtering, etc.
In the process, you end up re-implementing run of the mill state handling logic all over the place—whether with this.setState or by adopting some "state management framework" and writing the same boilerplate over and over again. And for your components to be nicely reusable across your application you augment them to handle both "controlled" and "uncontrolled" use cases using value or defaultValue. And to make things a bit more manageable, you re-invent common transforms like open, close, toggle, increment, decrement, etc. in lots of different components. And if you're working with a team, you end up doing all of this in slightly different ways throughout your codebase.
In the end, you're now maintaing a lot more logic than necessary, duplicated in many different places in slightly different ways. It gets harder and harder to understand your app's data flow. All while your app's bundle size grows.
react-values solves all of that with a few principles...
Principles
Leverage render props. It uses a render-prop-based API, exposing its state and a handful of convenient transform functions to you with the flexible function-as-children pattern.
Follow React's conventions. Its components follow React's own naming conventions, using familiar concepts like value/defaultValue. This makes it extremely easy to slot into existing codebases or frameworks.
Follow JavaScript's conventions. It exposes JavaScript's familiar, built-in methods like setDate/setHours, push/pop, filter, concat, etc. to avoid reinventing the wheel and forcing you to constantly read documentation.
Be extremely lightweight. It's extremely lightweight (and tree-shakeable), with most components weighing just a few hundred bytes, so you can even import it from public-facing component libraries.
Prioritize convenience. It's designed to provide convenient functions like increment, toggle, and smarter ones like incrementDate, decrementMonth, so you can build complex interactions in just a few lines of code.
Examples
To get a sense for how you might use react-values, check out a few of the examples:
Basic Toggle — using a Boolean to create a simple toggle component.
Reusable Toggle — showing how you might turn that toggle into a controlled component in your own UI kit.
Counter — a simple counter using a Number and its convenience transforms.
Connected Counters — two counters that are connected together, sharing a single value.
Time Picker — a more complex time picker component, using Date and its convenience transforms.
Filtering — a basic String value used for filtering a list.
Checkbox Set — using a Set to keep track of a checkbox group.
Simple Tooltip — a simplistic tooltip implemented as a Boolean.
Simple Modal — a simplistic modal implemented as a Boolean.
Connected Modal — a modal whose opened/closed state is controllable from other components.
If you have an idea for an example that shows a common use case, pull request it!
Documentation
If you're using react-values for the first time, check out the Getting Started guide to familiarize yourself with how it works. Once you've done that, you'll probably want to check out the full API Reference.
Getting Started
Installing react-values
Building a Component
Introducing State
Observing Changes
Setting Defaults
Controlled vs. Uncontrolled
Spreading Props
Reference
Components
<Value>
<ArrayValue>
<BooleanValue>
<DateValue>
<MapValue>
<NumberValue>
<ObjectValue>
<SetValue>
<StringValue>
Connected Components
createValue
createArrayValue
createBooleanValue
createDateValue
createMapValue
createNumberValue
createObjectValue
createSetValue
createStringValue
If even that's not enough, you can always read the source itself.
There are also translations of the documentation into other languages:
中文 (Chinese)
If you're maintaining a translation, feel free to pull request it here!
Contributing!
All contributions are super welcome! Check out the Contributing instructions for more info!
ianstormtaylor/react-values thuộc nhóm Frontend trên TopGit, cùng 5 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ề ianstormtaylor/react-values ở đâ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/ianstormtaylor/react-values là nguồn chính thức.
ianstormtaylor/react-values có bao nhiêu sao?
ianstormtaylor/react-values có 1.0k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/ianstormtaylor/react-values. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
ianstormtaylor/react-values có những chủ đề gì?
GitHub topics của ianstormtaylor/react-values: "components", "headless", "react", "render-props", "state-management". TopGit xếp repo vào nhóm Frontend.
ianstormtaylor/react-values còn đang phát triển không?
Commit gần nhất trên ianstormtaylor/react-values là 7.6 năm trước (theo timestamp GitHub). Repo có 31 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
ianstormtaylor/react-values viết bằng ngôn ngữ gì?
ianstormtaylor/react-values chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao ianstormtaylor/react-values được xếp vào nhóm Frontend?
TopGit xếp ianstormtaylor/react-values vào nhóm Frontend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "components", "headless", "react"). 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.
react-values có đáng để bạn bỏ thời gian?
ChatGPT, Claude và Perplexity đều đọc được trang này. Hỏi thử xem họ nghĩ gì về react-values.