Created
March 22, 2019 17:17
-
-
Save dacanizares/2684684c5b7a9031570a4ef501e8f579 to your computer and use it in GitHub Desktop.
A short and condensed example using ReactJS and Redux
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 { render } from 'react-dom'; | |
import { createStore, combineReducers } from 'redux' | |
import { Provider, connect } from 'react-redux' | |
import Hello from './Hello'; | |
import './style.css'; | |
const CHANGENAME = 'CHANGENAME' | |
const action = (name) => ({ | |
type: CHANGENAME, | |
payload: name | |
}) | |
const name = (state={}, action) => { | |
switch(action.type){ | |
case CHANGENAME: | |
return {...state, value: action.payload} | |
default: | |
return state | |
} | |
} | |
const reducers = combineReducers( | |
{name} | |
) | |
class App extends Component { | |
render() { | |
console.log(this.props) | |
return ( | |
<div> | |
<Hello name={this.props.name} /> | |
<p> | |
Start editing to see some magic happen :) | |
</p> | |
<button onClick={() => this.props.action('name')}>Click me</button> | |
</div> | |
); | |
} | |
} | |
const mapStateToProps = (state) => ({ | |
name: state.name.value | |
}) | |
const Apps = connect( | |
mapStateToProps, | |
{action} | |
)(App) | |
const store = createStore(reducers) | |
render( | |
<Provider store={store}> | |
<Apps /> | |
</Provider>, | |
document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment