Redux без копипаста

Содержание

Слайд 2

Pavel Uvarov, Software Engineer at Epam Ryazan pavel_uvarov@epam.com spiderpoul poul_uvarov

Pavel Uvarov,
Software Engineer at Epam Ryazan
pavel_uvarov@epam.com

spiderpoul

poul_uvarov

Слайд 3

Redux is a predictable state container

Redux is a predictable state container 

Слайд 4

Слайд 5

What I like about Redux? Predictable state management Global state Immutability

What I like about Redux?

Predictable state management
Global state
Immutability
A lot of middleware


Easy to use
Easy to test
Time traveling
Dev tools
Big community
Слайд 6

What we don’t like about Redux "Configuring a Redux store is

What we don’t like about Redux

"Configuring a Redux store is too

complicated"
"I have to add a lot of packages to get Redux to do anything useful"
"Redux requires too much boilerplate code"
Слайд 7

Post message { type: “SHOW_SUPPORT_PAGE”, payload: { … } } Iframe React App

Post message
{
type: “SHOW_SUPPORT_PAGE”,
payload: { … }
}

Iframe React App

Слайд 8

This tool will change the way you work with Redux 2-3x

This tool will change the way you work with Redux

2-3x less

code
Strictly typed out of the box
Reducer and action creation all in one
You won’t hate Redux so much after
Слайд 9

Special thanks to Yakov Zhmurov

Special thanks to Yakov Zhmurov

Слайд 10

React Redux Typescript + NASA API (nasa-app-react.netlify.app)

React Redux Typescript + NASA API (nasa-app-react.netlify.app)

Слайд 11

Слайд 12

Too much boilerplate…

Too much boilerplate…

Слайд 13

Слайд 14

Less pain with React Redux Hooks

Less pain with React Redux Hooks

Слайд 15

Even more with Typescript…

Even more with Typescript…

Слайд 16

Слайд 17

Слайд 18

redux-act / redux-actions

redux-act / redux-actions

Слайд 19

Solutions to reduce boilerplate Search Page store (Typescript)

Solutions to reduce boilerplate

Search Page store (Typescript)

Слайд 20

Redux Toolkit (@reduxjs/toolkit ) createSlice - a function that accepts an

Redux Toolkit (@reduxjs/toolkit )

createSlice - a function that accepts an initial

state, an object full of reducer functions, and a "slice name", and automatically generates action creators and action types that correspond to the reducers and state.
Слайд 21

@reduxjs/toolkit - createSlice

@reduxjs/toolkit - createSlice

Слайд 22

Solutions to reduce boilerplate Search Page store (Typescript)

Solutions to reduce boilerplate

Search Page store (Typescript)

Слайд 23

@reduxjs/toolkit – createSlice + createAsyncThunk dispatch(fetchSearchData(‘sun’))

@reduxjs/toolkit – createSlice + createAsyncThunk

dispatch(fetchSearchData(‘sun’))

Слайд 24

Redux Toolkit Type safety

Redux Toolkit Type safety

Слайд 25

Solutions to reduce boilerplate Search Page store (Typescript)

Solutions to reduce boilerplate

Search Page store (Typescript)

Слайд 26

Elm is a functional language that compiles to JavaScript. It has

Elm is a functional language that compiles to JavaScript. 
It has a

strong emphasis on simplicity and quality tooling.
Слайд 27

State management with Elm

State management with Elm

Слайд 28

ELM: update : Msg -> Model -> Model JS: const update

ELM:
update : Msg -> Model -> Model
JS:
const update = action

=> State => NewState
Слайд 29

Wouldn’t that be great…. const initialState = { name: ‘’, password:

Wouldn’t that be great….

const initialState = {
name: ‘’,
password: ‘’
}
const setName =

({ value }) => state => ({ …state, name: value })
const setPassword = ({ value }) => state => ({ …state, password : value })
Слайд 30

Wouldn’t that be even greater…. Reducer Action Creators Types inference

Wouldn’t that be even greater….

Reducer
Action Creators
Types inference

Слайд 31

Redux-blaze { type: “MY_FILTER_SET_CATEGORY” category: ‘my category’ }

Redux-blaze

{
type: “MY_FILTER_SET_CATEGORY”
category: ‘my category’
}

Слайд 32

redux-blaze buildReducer reducer – ready to use autogenerated reducer actionCreators –

redux-blaze buildReducer

reducer – ready to use autogenerated reducer
actionCreators – ready to

use strictly typed actions
bind – return function for binding actions to dispatch
Слайд 33

Easy to create High-order Reducers

Easy to create High-order Reducers

Слайд 34

createReduxFetcher

createReduxFetcher

Слайд 35

react-redux-blaze - createReduxFetcher All data keeps in redux global store Strictly

react-redux-blaze - createReduxFetcher

All data keeps in redux global store
Strictly typed
Autogenerated reducer
Handles

loading and error states
useData hook
Built in data selector
Prefetching data
Слайд 36

Another example “Earth page”

Another example “Earth page”

Слайд 37

Слайд 38

Redux-blaze

Redux-blaze

Слайд 39

Solutions to reduce boilerplate Search Page store (Typescript)

Solutions to reduce boilerplate

Search Page store (Typescript)

Слайд 40

After refactoring with redux-blaze

After refactoring with redux-blaze

Слайд 41

createSlice vs redux-blaze Pros: Customization Cons: Overcomplicated typing for TS Poor

createSlice vs redux-blaze

Pros:
Customization
Cons:
Overcomplicated typing for TS
Poor type inference
Complicated configs
Read

a lot of documentation before use
Слайд 42

Redux-blaze Easy to understand and use Easy to use with Typescript

Redux-blaze

Easy to understand and use
Easy to use with Typescript
Intellisense works even

with plain JS!
2-3x less code
Strictly typed out of the box
Light-weight!
Слайд 43

Слайд 44

Is Redux alive?

Is Redux alive?

Слайд 45

Слайд 46

Should we use Redux in 2020 for React Apps? I’d use

Should we use Redux in 2020 for React Apps?

I’d use Redux

(with redux-blaze) for:
complex apps (a lot of forms, filters and etc.) and teams more 4-5 members
Instead of multiple React Contexts
If no reason for Redux:
React Hooks
Hooks for Fetching (SWR, React Query)
Слайд 47

redux-blaze https://github.com/spiderpoul/redux-blaze https://github.com/spiderpoul/react-redux-blaze Nasa App https://nasa-app-react.netlify.app/ https://github.com/spiderpoul/nasa-app-react/tree/redux-blaze https://github.com/spiderpoul/nasa-app-react/tree/redux https://github.com/zeit/swr https://github.com/tannerlinsley/react-query

redux-blaze
https://github.com/spiderpoul/redux-blaze
https://github.com/spiderpoul/react-redux-blaze
Nasa App
https://nasa-app-react.netlify.app/
https://github.com/spiderpoul/nasa-app-react/tree/redux-blaze
https://github.com/spiderpoul/nasa-app-react/tree/redux
https://github.com/zeit/swr
https://github.com/tannerlinsley/react-query