Last active
May 2, 2021 08:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
counter: 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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