UNPKG

8.04 kBMarkdownView Raw
1# <a href='https://redux.js.org'><img src='https://camo.githubusercontent.com/f28b5bc7822f1b7bb28a96d8d09e7d79169248fc/687474703a2f2f692e696d6775722e636f6d2f4a65567164514d2e706e67' height='60' alt='Redux Logo' aria-label='redux.js.org' /></a>
2
3Redux is a predictable state container for JavaScript apps.
4
5It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as [live code editing combined with a time traveling debugger](https://github.com/reduxjs/redux-devtools).
6
7You can use Redux together with [React](https://react.dev), or with any other view library. The Redux core is tiny (2kB, including dependencies), and has a rich ecosystem of addons.
8
9[**Redux Toolkit**](https://redux-toolkit.js.org) is our official recommended approach for writing Redux logic. It wraps around the Redux core, and contains packages and functions that we think are essential for building a Redux app. Redux Toolkit builds in our suggested best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.
10
11![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/reduxjs/redux/test.yaml?branch=master&event=push&style=flat-square)
12[![npm version](https://img.shields.io/npm/v/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux)
13[![npm downloads](https://img.shields.io/npm/dm/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux)
14[![redux channel on discord](https://img.shields.io/badge/discord-%23redux%20%40%20reactiflux-61dafb.svg?style=flat-square)](https://discord.gg/0ZcbPKXt5bZ6au5t)
15
16## Installation
17
18### Create a React Redux App
19
20The recommended way to start new apps with React and Redux Toolkit is by using [our official Redux Toolkit + TS template for Vite](https://github.com/reduxjs/redux-templates), or by creating a new Next.js project using [Next's `with-redux` template](https://github.com/vercel/next.js/tree/canary/examples/with-redux).
21
22Both of these already have Redux Toolkit and React-Redux configured appropriately for that build tool, and come with a small example app that demonstrates how to use several of Redux Toolkit's features.
23
24```bash
25# Vite with our Redux+TS template
26# (using the `degit` tool to clone and extract the template)
27npx degit reduxjs/redux-templates/packages/vite-template-redux my-app
28
29# Next.js using the `with-redux` template
30npx create-next-app --example with-redux my-app
31```
32
33We do not currently have official React Native templates, but recommend these templates for standard React Native and for Expo:
34
35- https://github.com/rahsheen/react-native-template-redux-typescript
36- https://github.com/rahsheen/expo-template-redux-typescript
37
38```
39npm install @reduxjs/toolkit react-redux
40```
41
42For the Redux core library by itself:
43
44```
45npm install redux
46```
47
48For more details, see [the Installation docs page](https://redux.js.org/introduction/installation).
49
50## Documentation
51
52The Redux core docs are located at **https://redux.js.org**, and include the full Redux tutorials, as well usage guides on general Redux patterns:
53
54- [Introduction](https://redux.js.org/introduction/getting-started)
55- [Tutorials](https://redux.js.org/tutorials/index)
56- [Usage Guides](https://redux.js.org/usage/index)
57- [FAQ](https://redux.js.org/faq)
58- [API Reference](https://redux.js.org/api/api-reference)
59
60The Redux Toolkit docs are available at **https://redux-toolkit.js.org**, including API references and usage guides for all of the APIs included in Redux Toolkit.
61
62## Learn Redux
63
64### Redux Essentials Tutorial
65
66The [**Redux Essentials tutorial**](https://redux.js.org/tutorials/essentials/part-1-overview-concepts) is a "top-down" tutorial that teaches "how to use Redux the right way", using our latest recommended APIs and best practices. We recommend starting there.
67
68### Redux Fundamentals Tutorial
69
70The [**Redux Fundamentals tutorial**](https://redux.js.org/tutorials/fundamentals/part-1-overview) is a "bottom-up" tutorial that teaches "how Redux works" from first principles and without any abstractions, and why standard Redux usage patterns exist.
71
72### Help and Discussion
73
74The **[#redux channel](https://discord.gg/0ZcbPKXt5bZ6au5t)** of the **[Reactiflux Discord community](https://www.reactiflux.com)** is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - please come and join us there!
75
76## Before Proceeding Further
77
78Redux is a valuable tool for organizing your state, but you should also consider whether it's appropriate for your situation. Please don't use Redux just because someone said you should - instead, please take some time to understand the potential benefits and tradeoffs of using it.
79
80Here are some suggestions on when it makes sense to use Redux:
81
82- You have reasonable amounts of data changing over time
83- You need a single source of truth for your state
84- You find that keeping all your state in a top-level component is no longer sufficient
85
86Yes, these guidelines are subjective and vague, but this is for a good reason. The point at which you should integrate Redux into your application is different for every user and different for every application.
87
88> **For more thoughts on how Redux is meant to be used, please see:**<br>
89>
90> - **[When (and when not) to reach for Redux](https://changelog.com/posts/when-and-when-not-to-reach-for-redux)**
91> - **[You Might Not Need Redux](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)**<br>
92> - **[The Tao of Redux, Part 1 - Implementation and Intent](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-1/)**<br>
93> - **[The Tao of Redux, Part 2 - Practice and Philosophy](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-2/)**
94> - **[Redux FAQ](https://redux.js.org/faq)**
95
96## Basic Example
97
98The whole global state of your app is stored in an object tree inside a single _store_.
99The only way to change the state tree is to create an _action_, an object describing what happened, and _dispatch_ it to the store.
100To specify how state gets updated in response to an action, you write pure _reducer_ functions that calculate a new state based on the old state and the action.
101
102Redux Toolkit simplifies the process of writing Redux logic and setting up the store. With Redux Toolkit, the basic app logic looks like:
103
104```js
105import { createSlice, configureStore } from '@reduxjs/toolkit'
106
107const counterSlice = createSlice({
108 name: 'counter',
109 initialState: {
110 value: 0
111 },
112 reducers: {
113 incremented: state => {
114 // Redux Toolkit allows us to write "mutating" logic in reducers. It
115 // doesn't actually mutate the state because it uses the Immer library,
116 // which detects changes to a "draft state" and produces a brand new
117 // immutable state based off those changes
118 state.value += 1
119 },
120 decremented: state => {
121 state.value -= 1
122 }
123 }
124})
125
126export const { incremented, decremented } = counterSlice.actions
127
128const store = configureStore({
129 reducer: counterSlice.reducer
130})
131
132// Can still subscribe to the store
133store.subscribe(() => console.log(store.getState()))
134
135// Still pass action objects to `dispatch`, but they're created for us
136store.dispatch(incremented())
137// {value: 1}
138store.dispatch(incremented())
139// {value: 2}
140store.dispatch(decremented())
141// {value: 1}
142```
143
144Redux Toolkit allows us to write shorter logic that's easier to read, while still following the original core Redux behavior and data flow.
145
146## Logo
147
148You can find the official logo [on GitHub](https://github.com/reduxjs/redux/tree/master/logo).
149
150## Change Log
151
152This project adheres to [Semantic Versioning](https://semver.org/).
153Every release, along with the migration instructions, is documented on the GitHub [Releases](https://github.com/reduxjs/redux/releases) page.
154
155## License
156
157[MIT](LICENSE.md)