Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created March 6, 2020 18:22
Show Gist options
  • Select an option

  • Save r3dm1ke/ab0620d790e28839ba4b571ce68ca67e to your computer and use it in GitHub Desktop.

Select an option

Save r3dm1ke/ab0620d790e28839ba4b571ce68ca67e to your computer and use it in GitHub Desktop.
// Example with Redux Toolkit
import React from 'react';
import {createAction, createReducer, configureStore} from 'redux-toolkit';
import {Provider, useSelector, useDispatch} from 'react-redux';
const initialState = {counter: 0};
const increment = createAction('INCREMENT');
const reducer = createReducer(initialState, {
[increment.type]: state => ({...state, counter: state.counter + 1})
});
const store = configureStore({reducer: counter});
const App = () => {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}
const Counter = () => {
const counter = useSelector(state => state.counter);
const dispatch = useDispatch();
return (
<>
<button onClick={() => dispatch(increment)}>Click me</button>
<p>You clicked me {counter} times</p>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment