Skip to content

Instantly share code, notes, and snippets.

@eduard-tkv
Created May 3, 2017 19:22
Show Gist options
  • Save eduard-tkv/81858fca71f07abdb9d4f852b09c7426 to your computer and use it in GitHub Desktop.
Save eduard-tkv/81858fca71f07abdb9d4f852b09c7426 to your computer and use it in GitHub Desktop.
//components/CountWidget.js
import React from 'react';
let CountWidget = ({count, handleClick})=>{
return(
<div>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment</button>
</div>
)
};
export default CountWidget;
// containers/CountWidget.js
import React, { Component } from 'react';
import {incrementCount} from '../actions';
import CountWidget from '../components/CountWidget';
export default class CounterWidgetContainer extends React.Component {
constructor(props){
super(props);
this.state = { count: props.store.getState() };
this.handleClick = this.handleClick.bind(this);
this.handleChange = this.handleChange.bind(this);
}
componentDidMount(){ this.props.store.subscribe(this.handleChange); }
handleChange(){ this.setState({ count: this.props.store.getState() });
}
handleClick(){ this.props.store.dispatch(incrementCount()); }
render(){
return (
<CountWidget count={this.state.count} handleClick={this.handleClick}/>
);
}
}
// App.js
import React, { Component } from 'react';
import {incrementCount} from '../actions';
import CountWidget from '../containers/CountWidget';
export default class App extends React.Component {
render(){
return (
<div>
<CountWidget store={this.props.store}/>
</div>
);
}
}
//index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import App from './components/App';
import reducer from './reducers';
let store = createStore(reducer);
ReactDOM.render(<App store={store}/>, document.getElementById('app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment