If you're using redux today, you should be using redux-toolkit

πŸ”₯ Learn redux-toolkit in 10 tweets πŸ‘‡
1/10

Create a "store" with "configureStore"

A store holds all your data and the actions that change that data

Use a "Provider" component (like context) to give the store to all your components import { configureStore } from '@reduxjs/toolkit'; import { All your data and actions to update that data  Divided into
2/10

Create named "slices"

A slice holds a chunk of the state, plus the functions that can update that state

No more changing 4 different files just to write one action! πŸŽ‰ export const counterSlice = createSlice({   name: 'counter',
3/10

Add the slice to your store by adding the "reducer" from the slice to the "reducer" in the store

The slice ".reducer" (singular) is auto-created when you define your "reducers:" (plural) in the slice const store = configureStore({   reducer: {     counter: cou
4/10

Functions that update the state in your slice are "reducers"

A reducer takes the current state and the action (data) being performed,

and updates the data in the slice's state

(redux-toolkit uses immer, so those state changes are actually non-mutating) export const counterSlice = createSlice({   name: 'counter',
5/10

To get values from the store in a component, use a "selector"

A selector function is given the entire redux store and returns just the data you want

Pass your selector to the "useSelector" hook which will call it for you import { useSelector } from 'react-redux';  ...  const App =
6/10

To change data in the store, use the "actions" that are exported from the slice

These actions are named based on your named reducers

You don't call on their own however, because they need the current state and action params const App = () => {    const actions = counterSlice.actions
7/10

To call the action, you "dispatch" it to the store

Use the "useDispatch" hook to get access to the "dispatch" function

then dispatch your action using that function

this automatically updates all components using "useSelector" const App = () => {    const count = useSelector(state => st
8/10

Actions can take params as well, which are passed in as the second argument to the reducer

the action "payload" will contain whatever is passed to the action

(to pass multiple params, use an object) export const counterSlice = createSlice({   name: 'counter',const App = () => {   const count = useSelector(state => sta
9/10

For async actions (like fetching data), you can use redux-thunk (already included!)

Create a thunk (function that returns a function)

it's async and gets dispatch as a param

dispatch inside your thunk function

Then dispatch that action like normal in component const fetchLength = () => async dispatch => {   const resultconst App = () => {    const count = useSelector(state => st
10/10

What about just using Context instead?

Yes, I generally start projects with just context, but redux can have some advantages for larger projects or teams:

- provides structure that context doesn't
- can be easier to test
- redux dev tools are pretty cool πŸ’―
Resources:

Code for this example:
github.com/chrisachard/re…

Quick start redux-toolkit docs:
redux-toolkit.js.org/introduction/q…

Redux devtools chrome extension:
github.com/zalmoxisus/red…

The current redux maintainer and general keeper of many links:
@acemarke
Gah! that second .up should be .down

Thanks @NanaGilbertB for pointing that out!πŸ™ŒπŸ™Œ

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Chris Achard

Chris Achard Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @chrisachard

28 Oct 19
Confused by DNS Records? A, CNAME, ALIAS...

Me too 😬

Let's change that! (cheatsheet at the end)

πŸ”₯ DNS Record crash course for web developers πŸ‘‡
1/10

To get to example.com, your computer needs to know where on the Internet that server is

That means: it has to know what IP address to talk to

The way it finds out is the Domain Name System (DNS)
2/10

When you register a domain, you can set many types of DNS Records

Each record has a Type, a Host, and a Value

- "Types" are predefined
- "Host" represents the root (@) or a subdomain (www)
- "Value" is an IP or web address, or other value
Read 12 tweets
8 Oct 19
πŸ”₯ Intro to regex for web developers πŸ‘‡
1/10

Regular expressions find parts of a string that match a pattern

In JS they're created in between forward slashes //, or with `new RegExp()`

and then used in methods like `match`, `test`, or `replace`

You can define the regex beforehand or directly when calling the method
2/10

Match individual characters one at a time,

or put multiple characters in square brackets [] to capture any that match

Capture a range of characters with a hyphen -
Read 12 tweets
3 Oct 19
A thread full of free resources for learning React πŸ‘‡
1: First: the React docs are really great!

There's even a tutorial as part of the official docs, which walks you through building a tic-tac-toe game:

reactjs.org/tutorial/tutor…
2: @kentcdodds has a great free video course on egghead for beginners:

egghead.io/courses/the-be…
Read 8 tweets
1 Oct 19
Ok; it's #redux time:

(code link at the end)

πŸ”₯ Learn Redux in 10 tweets - with React and hooks! 🎣 πŸ‘‡
1/10

Redux gives you a central place to put your state (data) for JavaScript apps

It's most often used with React (via react-redux)

This lets you access or change your state from ANY component in your tree
2/10

Your state lives in a central Redux store

That store is created with a function called a reducer

A reducer takes in a state and an action,
and returns the same or a NEW state
Read 12 tweets
20 Sep 19
πŸ”₯ Learn React in 10 tweets (with hooks) πŸ‘‡
1/10

How React Works:

- you display data on a webpage
- a user interacts with it
- now the data changes...
- ...and you want the webpage to look different

React does that for you! πŸ’―
2/10

To think in React:

Break your UI into custom components.

Each component is responsible for displaying itself, based on the _external_ and _internal_ data available.

Build trees of these components for a full UI
Read 12 tweets
17 Sep 19
Have you heard about map, filter, and reduce, but never really understood them? πŸ€”

Here's a πŸ”₯ map().filter().reduce() πŸ”₯ crash course for you!

(sample code at the end)

THREAD πŸ‘‡
1/10

`map`, `filter` and `reduce` can all operate independently, or be chained together

They operate on an *array* and *transform* it
2/10

`filter` takes the array and returns a new array that only contains the elements that match some condition
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!