create-react-app中使用redux,react-redux

1,index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import reducer from './index.reducer'

//创建store
const store = createStore(reducer);


ReactDOM.render(
    
        
    , 
    document.getElementById('root'));

2,index.reducer.js

const tiger = 100


//这是action
const increase = {
    type: '加'
}
const decrease = {
    type: '减'
}
//这是reducer
const reducer = (state = tiger, action) => {
    switch (action.type) {
        case '加':
		   

			return  state +=100
        case '减':
            return  state -=100
        default:
            return state;
    }
}
export default reducer

3,APP.JS

import React, { Component } from 'react';
import { connect } from 'react-redux';

class App extends Component {

  componentDidMount() {
    console.log('this.props====',this.props.tiger)
  } 
  render() {
    const { PayIncrease, PayDecrease } = this.props;
    return (
      

{this.props.tiger}

); } } //需要渲染什么数据 function mapStateToProps(state) { return { tiger: state } } //需要触发什么行为 function mapDispatchToProps(dispatch) { return { PayIncrease: () => dispatch({ type: '加' }), PayDecrease: () => dispatch({ type: '减' }) } } export default App = connect(mapStateToProps, mapDispatchToProps)(App)

 

你可能感兴趣的:(react,react-redux)