Open mianmalife opened 4 years ago
`import React from "react"; import { render } from "react-dom"; import thunk from "redux-thunk"; import { createStore, applyMiddleware } from "redux"; import { Provider } from "react-redux"; import Counter from "./Counter"; import "./index.css";
const initialState = { count: 0 };
function reducer(state = initialState, action) { //接收state,action ==> newState reducer 绝不能返回 undefined switch (action.type) { case "INCREMENT": return { count: state.count + 1 }; case "DECREMENT": return { count: state.count ? state.count - 1 : 0 }; case "RESET": return { count: 0 }; default: return state; } } const store = createStore(reducer, applyMiddleware(thunk));
const App = () => (
);
render(, document.getElementById("root")); `
https://codesandbox.io/s/funny-lake-kgqvj
`import React from "react"; import { render } from "react-dom"; import thunk from "redux-thunk"; import { createStore, applyMiddleware } from "redux"; import { Provider } from "react-redux"; import Counter from "./Counter"; import "./index.css";
const initialState = { count: 0 };
function reducer(state = initialState, action) { //接收state,action ==> newState reducer 绝不能返回 undefined switch (action.type) { case "INCREMENT": return { count: state.count + 1 }; case "DECREMENT": return { count: state.count ? state.count - 1 : 0 }; case "RESET": return { count: 0 }; default: return state; } } const store = createStore(reducer, applyMiddleware(thunk));
const App = () => (
);
render( , document.getElementById("root"));
`