Skip to content

Commit

Permalink
Changed from connect to Hooks ⚑ (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
troglodytto authored and jmyrland committed Sep 16, 2022
1 parent 6f1cf4d commit c48a23d
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions website/docs/docs/recipes/usage-with-react-redux.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ As Easy Peasy outputs a standard Redux store, so it is entirely possible to use
This allows you to do a few things:

- Slowly migrate a legacy application that is built using `react-redux`
- Connect your store to Class components via `connect`
- Connect your store to Class components via `useDispatch and useSelectors`

**1. First, install the `react-redux` package**

Expand Down Expand Up @@ -36,27 +36,25 @@ const App = () => (
render(<App />, document.querySelector('#app'));
```

**3. Finally, use `connect` against your components**
**3. Finally, use `useSelector` and `useDispatch` against your components**

```javascript
import React, { Component } from 'react';
import { connect } from 'react-redux'; // πŸ‘ˆ import the connect
import React from 'react';
import { useSelector, useStoreActions } from "./model"; // πŸ‘ˆ For connecting the store with the components and dispatching actions

function TodoList({ todos, addTodo }) {
const TodoList = ({ todos, addTodo }) => {
const todos = useSelector(state => state.todos.items);
const addTodo = useStoreActions(dispatch => dispatch.todos.addTodo);

return (
<div>
{todos.map(({ id, text }) => (
{todos.map(({ id, text }) =>
<Todo key={id} text={text} />
))}
)}
<AddTodo onSubmit={addTodo} />
</div>
);
}

export default connect(
// πŸ‘‡ Map to your required state
(state) => ({ todos: state.todos.items }),
// πŸ‘‡ Map your required actions
(dispatch) => ({ addTodo: dispatch.todos.addTodo }),
)(TodoList);
export default TodoList;
```

0 comments on commit c48a23d

Please sign in to comment.