Skip to content

Instantly share code, notes, and snippets.

@hanbzu
Last active May 2, 2021 08:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanbzu/becf4cf24ec2a099a26a to your computer and use it in GitHub Desktop.
Save hanbzu/becf4cf24ec2a099a26a to your computer and use it in GitHub Desktop.
Proposal for a React component with state managed by actions and a reducer.
import React, { Component } from 'react'
import initialState from './initialState.js'
import reducer from './reducer.js'
export class Layout extends Component {
state = initialState
reduce = (action) =>
this.setState(reducer(action, this.state))
render() {
return (
<div>
<h1>Placeholder</h1>
<button onClick={() => this.reduce({ type: 'INCREMENT' })} />
</div>
)
}
}
export default {
counter: 0
}
export default function(action, state) {
switch (action.type) {
case 'INCREMENT':
return { ...state, counter: state.counter + 1 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment