安装redux和react redux
index.js 用react-redux的Provider复合组件实现逐层传参,导入store作为要传的参数
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { Provider } from "react-redux";
import store from "./store";
ReactDOM.render(
,
document.getElementById("root"),
);
src/store/index.js 使用redux的createStore和combineReducers,定义了reducer,传给store
import { createStore, combineReducers } from "redux";
const initialUser = {
isLogin: false,
user: { name: null },
};
function loginReducer(state = { ...initialUser }, action) {
switch (action.type) {
case "getUserInfo":
return { ...initialUser };
case "loginSucce