纯redux 传数据

index.js:


import App from './App';
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { reducer } from './container/appDux.js';
const store = createStore( reducer);

function render() {
ReactDOM. render((
< App store= { store } />
), document. getElementById( 'root'));
}
render();
store. subscribe( render);


App.js

import React, { Component } from 'react';
import { ADDGUN} from './container/appDux'

class App extends Component {
render() {
const { store } = this. props;
const num = store. getState();
return (
< div >
< div >现在有机枪 { num } div >
< button onClick= {() => store. dispatch( ADDGUN()) } >点击加 button >
div >
);
}
}

export default App;


appDux.js


export function reducer( state = 0, action) {
switch ( action. type) {
case 'ADD':
return state + 1;
case 'REMOVE':
return state + 1;
default:
return 10;
}
}

export function ADDGUN() {
return {
type: 'ADD'
}
}

export function REMOVEFUN() {
return {
type: 'REMOVE'
}
}

你可能感兴趣的:(react)