Skip to content

Instantly share code, notes, and snippets.

@brunosabot
Last active July 19, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunosabot/31b74e415c0709fbd48120aac58885d1 to your computer and use it in GitHub Desktop.
Save brunosabot/31b74e415c0709fbd48120aac58885d1 to your computer and use it in GitHub Desktop.
/* actions/beer.js */
export const TOGGLE_AVAILABILITY_FOR_BEER = 'TOGGLE_AVAILABILITY_FOR_BEER';
/* dispatchers/beer.js */
import {TOGGLE_AVAILABILITY_FOR_BEER} from '../actions/beer.js';
export const toggleBeerAvailability = () => ({
type: TOGGLE_AVAILABILITY_FOR_BEER
});
/* reducers/beer.js */
import {TOGGLE_AVAILABILITY_FOR_BEER} from '../actions/beer.js';
const beer = (state = {availableForBeer: true}, action) => {
switch (action.type) {
case TOGGLE_AVAILABILITY_FOR_BEER:
return {...state, availableForBeer: !state.availableForBeer};
default:
return state
}
};
export default beer;
/* components/beer.jsx */
import React from 'react';
import { connect } from 'react-redux';
import { toggleBeerAvailability } from '../dispatchers/beer';
const Beer = ({ isAvailable, toggleAvailability }) => (
<>
<div>
I'm currently
{isAvailable ? ' ' : ' not '}
available for a beer
</div>
<button onClick={toggleAvailability}>Change</button>
</>
);
const mapStateToProps = state => ({
isAvailable: state.availableForBeer
});
const mapDispatchToProps = dispatch => ({
toggleAvailability: () => dispatch(toggleBeerAvailability(id))
});
export default connect(mapStateToProps, mapDispatchToProps)(Beer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment