-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React-Redux #12
Comments
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
//import App from './App';
import registerServiceWorker from './registerServiceWorker';
import {store} from './store'
import {Provider} from 'react-redux'
import Counter2 from './components/counter2'
ReactDOM.render(
<Provider store={store}>
<Counter2/>
</Provider>,
document.getElementById('root')
);
registerServiceWorker();
import {createStore} from 'redux'
import counter2 from './reducers/counter2'
const store = createStore(counter2)
export {store}
import React, {Component} from 'react'
import {increaseAction} from "../actions/increase";
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
class Counter2 extends Component {
render() {
const {value, onIncreaseClick} = this.props
return (
<div class="counter2">
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
</div>
)
}
}
Counter2.propTypes = {
value: PropTypes.number.isRequired,
onIncreaseClick: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
value: state.count
}
}
function mapDispatchToProps(disptch) {
return {
onIncreaseClick: () => {
disptch(increaseAction())
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Counter2)
export function increase() {
return {
type: 'increase'
}
}
export default (state = {count: 0}, action) => {
const count = state.count
switch (action.type) {
case 'increase':
return {count: count + 1}
default:
return state
}
} |
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
//import App from './App';
import registerServiceWorker from './registerServiceWorker';
import {store} from './store'
import {Provider} from 'react-redux'
import Counter2 from './components/counter2'
ReactDOM.render(
<Provider store={store}>
<Counter2/>
</Provider>,
document.getElementById('root')
);
registerServiceWorker();
import {createStore} from 'redux'
import counter2 from './reducers/counter2'
const store = createStore(counter2)
export {store}
import React, {Component} from 'react'
import * as actions from "../actions/increase";
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
class Counter2 extends Component {
handleClick() {
this.props.increase()
}
render() {
const {value} = this.props
return (
<div class="counter2">
<span>{value}</span>
<button onClick={this.handleClick.bind(this)}>Increase</button>
</div>
)
}
}
Counter2.propTypes = {
value: PropTypes.number.isRequired,
onIncreaseClick: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
value: state.count
}
}
let mapDispatchToProps = dispatch => bindActionCreators(actions, dispatch)
export default connect(mapStateToProps, mapDispatchToProps)(Counter2)
export function increase() {
return {
type: 'increase'
}
}
export default (state = {count: 0}, action) => {
const count = state.count
switch (action.type) {
case 'increase':
return {count: count + 1}
default:
return state
}
} |
Open
Open
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: