Skip to content

Instantly share code, notes, and snippets.

View maximiliangonzalez's full-sized avatar

Maximilian Gonzalez maximiliangonzalez

  • Volta Charging
  • San Francisco
View GitHub Profile
@maximiliangonzalez
maximiliangonzalez / App.jsx
Last active July 24, 2019 20:32
React functional component connecting to Redux store using useSelector hook (still using connect for mapDispatchToProps)
import React from 'react';
import {useSelector, connect} from 'react-redux';
import * as actions from '../actions/actions';
const App = props => {
const {increment, decrement} = props;
const count = useSelector(store => store.count);
return (
<div>
@maximiliangonzalez
maximiliangonzalez / App.jsx
Last active July 24, 2019 23:13
React component connecting to Redux store without hooks, converted into a functional component
import React from 'react';
import {connect} from 'react-redux';
import * as actions from '../actions/actions';
const App = props => {
const {count, increment, decrement} = props;
return (
<div>
<h1>The count is {count}</h1>
@maximiliangonzalez
maximiliangonzalez / App.jsx
Last active July 24, 2019 20:31
React component connecting to the Redux store using hooks (isn't this so much nicer?)
import React from 'react';
import * as actions from '../actions/actions';
import {useSelector, useDispatch} from 'react-redux';
const App = () => {
const dispatch = useDispatch();
const count = useSelector(store => store.count);
return (
<div>
@maximiliangonzalez
maximiliangonzalez / App.jsx
Last active July 24, 2019 23:13
React component connecting to the Redux store WITHOUT using React Redux hooks
import React from 'react';
import {connect} from 'react-redux';
import * as actions from '../actions/actions';
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
@maximiliangonzalez
maximiliangonzalez / index.js
Created July 24, 2019 18:50
The top layer of our React app, wrapped in a Provider so we can access the counter in our Redux store.
import React from 'react';
import {render} from 'react-dom';
import App from './components/App.jsx';
import {Provider} from 'react-redux';
import store from './reducers/counterReducer';
render(
<Provider store={store}>
<App />
</Provider>,
@maximiliangonzalez
maximiliangonzalez / counterReducer.js
Created July 24, 2019 18:47
A reducer to update the counter in our Redux store
import { createStore } from 'redux';
const initialState = {
count: 0
}
const counterReducer = (state = initialState, action) => {
switch(action.type) {
case 'CHANGE_COUNT':
return {
@maximiliangonzalez
maximiliangonzalez / actions.js
Created July 24, 2019 18:42
Action creators to control a counter
export const increment = count => ({
type: 'CHANGE_COUNT',
payload: count + 1
});
export const decrement = count => ({
type: 'CHANGE_COUNT',
payload: count - 1
});