|
| 1 | +--- |
| 2 | +id: getting-started |
| 3 | +title: Getting Started |
| 4 | +hide_title: true |
| 5 | +sidebar_label: Getting Started |
| 6 | +--- |
| 7 | + |
| 8 | +# Getting Started with React Redux |
| 9 | + |
| 10 | +[React Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) UI bindings layer for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update state. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +React Redux 7.1+ requires **React 16.8.3 or later**, in order to make use of React Hooks. |
| 15 | + |
| 16 | +### Using Create React App |
| 17 | + |
| 18 | +The recommended way to start new apps with React Redux is by using the [official Redux+JS template](https://github.com/reduxjs/cra-template-redux) for [Create React App](https://github.com/facebook/create-react-app), which takes advantage of [Redux Toolkit](https://redux-toolkit.js.org/). |
| 19 | + |
| 20 | +```sh |
| 21 | +npx create-react-app my-app --template redux |
| 22 | +``` |
| 23 | + |
| 24 | +### An Existing React App |
| 25 | + |
| 26 | +To use React Redux with your React app, install it as a dependency: |
| 27 | + |
| 28 | +```bash |
| 29 | +# If you use npm: |
| 30 | +npm install react-redux |
| 31 | + |
| 32 | +# Or if you use Yarn: |
| 33 | +yarn add react-redux |
| 34 | +``` |
| 35 | + |
| 36 | +You'll also need to [install Redux](https://redux.js.org/introduction/installation) and [set up a Redux store](https://redux.js.org/recipes/configuring-your-store/) in your app. |
| 37 | + |
| 38 | +If you are using TypeScript, the React Redux types are maintained separately in DefinitelyTyped. You'll need to install those as well: |
| 39 | + |
| 40 | +```bash |
| 41 | +npm install @types/react-redux |
| 42 | +``` |
| 43 | + |
| 44 | +## `Provider` |
| 45 | + |
| 46 | +React Redux includes a `<Provider />` component, which makes the Redux store available to the rest of your app: |
| 47 | + |
| 48 | +```js |
| 49 | +import React from 'react' |
| 50 | +import ReactDOM from 'react-dom' |
| 51 | + |
| 52 | +import { Provider } from 'react-redux' |
| 53 | +import store from './store' |
| 54 | + |
| 55 | +import App from './App' |
| 56 | + |
| 57 | +const rootElement = document.getElementById('root') |
| 58 | +ReactDOM.render( |
| 59 | + <Provider store={store}> |
| 60 | + <App /> |
| 61 | + </Provider>, |
| 62 | + rootElement |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +## Hooks |
| 67 | + |
| 68 | +React Redux provides a pair of custom React hooks that allow your React components to interact with the Redux store. |
| 69 | + |
| 70 | +`useSelector` reads a value from the store state and subscribes to updates, while `useDispatch` returns the store's `dispatch` method to let you dispatch actions. |
| 71 | + |
| 72 | +```js |
| 73 | +import React, { useState } from 'react' |
| 74 | +import { useSelector, useDispatch } from 'react-redux' |
| 75 | +import { |
| 76 | + decrement, |
| 77 | + increment, |
| 78 | + incrementByAmount, |
| 79 | + incrementAsync, |
| 80 | + selectCount |
| 81 | +} from './counterSlice' |
| 82 | +import styles from './Counter.module.css' |
| 83 | + |
| 84 | +export function Counter() { |
| 85 | + const count = useSelector(selectCount) |
| 86 | + const dispatch = useDispatch() |
| 87 | + const [incrementAmount, setIncrementAmount] = useState('2') |
| 88 | + |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <div className={styles.row}> |
| 92 | + <button |
| 93 | + className={styles.button} |
| 94 | + aria-label="Increment value" |
| 95 | + onClick={() => dispatch(increment())} |
| 96 | + > |
| 97 | + + |
| 98 | + </button> |
| 99 | + <span className={styles.value}>{count}</span> |
| 100 | + <button |
| 101 | + className={styles.button} |
| 102 | + aria-label="Decrement value" |
| 103 | + onClick={() => dispatch(decrement())} |
| 104 | + > |
| 105 | + - |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + {/* omit additional rendering output here */} |
| 109 | + </div> |
| 110 | + ) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Help and Discussion |
| 115 | + |
| 116 | +The **[#redux channel](https://discord.gg/0ZcbPKXt5bZ6au5t)** of the **[Reactiflux Discord community](http://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 - come join us! |
| 117 | + |
| 118 | +You can also ask questions on [Stack Overflow](https://stackoverflow.com) using the **[#redux tag](https://stackoverflow.com/questions/tagged/redux)**. |
| 119 | + |
| 120 | +## Docs Translations |
| 121 | + |
| 122 | +- [Portuguese](https://fernandobelotto.github.io/react-redux) |
0 commit comments