Skip to content

Instantly share code, notes, and snippets.

@dacanizares
Created March 22, 2019 17:17
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 dacanizares/2684684c5b7a9031570a4ef501e8f579 to your computer and use it in GitHub Desktop.
Save dacanizares/2684684c5b7a9031570a4ef501e8f579 to your computer and use it in GitHub Desktop.
A short and condensed example using ReactJS and Redux
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