Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save r3dm1ke/8436d692a07b17339e3d4b0005aa67f8 to your computer and use it in GitHub Desktop.
// EXAMPLE WITH REDUX
import React from 'react';
import {createStore} from 'redux';
import {Provider, useSelector, useDispatch} from 'react-redux';
const initialState = {counter: 0};
const reducer = (state=initialState, action) => {
switch (action.type) {
case 'INCREMENT':
return {...state, counter: state.counter + 1};
default:
return state;
}
};
const store = createStore(reducer);
const App = () => {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}
const Counter = () => {
const counter = useSelector(state => state.counter);
const dispatch = useDispatch();
return (
<>
<button onClick={() => dispatch({type: '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