npm i redux react-redux --save
// 定义状态
const defaultState = {
msg: "hello world!",
};
// 导出函数
export default (state = defaultState) => {
return state;
};
// eslint-disable-next-line
export default (state = defaultState) => {
return state;
};
import reducer from "./reducer";
import { createStore } from "redux";
const store = createStore(reducer);
export default store;
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App1";
import { Provider } from "react-redux";
import store from "./store";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
// 在这下面的都可以用store
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);
import React, { Fragment } from "react";
import { connect } from "react-redux";
function App1(props) {
return (
<Fragment>
<div>{props.msg}</div>
</Fragment>
);
}
// state映射
const mapStateToProps = (state) => {
return {
msg: state.msg,
};
};
// connect(state映射, dispatch映射)(当前组件类)
export default connect(mapStateToProps)(App1);
const mapDispatchToProps = (dispatch) => {
return {
// 这里和上面一样的key-value写法
// changeMsg() {}就是changeMsg: function() {}的简写
changeMsg(arg) {
const action = { type: "changeMsgFn", value: arg };
dispatch(action);
},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App1);
function App1(props) {
return (
<Fragment>
<div>{props.msg}</div>
<button onClick={() => props.changeMsg("你好世界!")}>修改msg</button>
</Fragment>
);
}
function App1(props) {
// 相当于在外面写
const fn = () => props.changeMsg("你好世界!");
return (
<Fragment>
<div>{props.msg}</div>
<button onClick={fn}>修改msg</button>
</Fragment>
);
}
function App1(props) {
return (
<Fragment>
<div>{props.msg}</div>
// 会立刻执行changeMsg函数,没有点击按钮,msg会立刻被你好世界替换
<button onClick={props.changeMsg("你好世界!")}>修改msg</button>
</Fragment>
);
}
<button onClick={props.changeMsg}>修改msg</button>
// 但是下面的方法也要修改成
changeMsg() {
const action = { type: "changeMsgFn", value: "不传参" };
dispatch(action);
},
import React, { Fragment } from "react";
import { connect } from "react-redux";
function App1(props) {
return (
<Fragment>
<div>{props.msg}</div>
<button onClick={() => props.changeMsg("你好世界!")}>修改msg</button>
</Fragment>
);
}
const mapStateToProps = (state) => {
return {
msg: state.msg,
};
};
const mapDispatchToProps = (dispatch) => {
return {
changeMsg(arg) {
const action = { type: "changeMsgFn", value: arg };
dispatch(action);
},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(App1)
const defaultState = {
msg: "hello world!",
};
// 这里接收了action,通过action.type判断方法,然后赋值将传的value赋值给msg
// eslint-disable-next-line
export default (state = defaultState, action) => {
// 这里深拷贝了一份state是因为不能直接修改state
let newState = JSON.parse(JSON.stringify(state));
switch (action.type) {
case "changeMsgFn":
newState.msg = action.value;
break;
default:
break;
}
return newState;
};
import React, { Fragment } from "react";
import { useSelector } from "react-redux";
function App1() {
const msg = useSelector((state) => state.msg);
// 假如多个变量的话可以解构赋值
// const { msg, msg2 } = useSelector((state) => state);
return (
<Fragment>
<div>{msg}</div>
</Fragment>
);
}
export default App1;
import React, { Fragment } from "react";
import { useSelector, useDispatch } from "react-redux";
function App1() {
const dispatch = useDispatch();
const msg = useSelector((state) => state.msg);
const changeMsg = () => {
dispatch({ type: "changeMsgFn", value: "你好世界!" });
};
return (
<Fragment>
<div>{msg}</div>
<button onClick={changeMsg}>修改msg</button>
</Fragment>
);
}
export default App1;
import React, { Fragment } from "react";
import { useSelector, useDispatch } from "react-redux";
function App1() {
const dispatch = useDispatch();
const { msg } = useSelector((state) => state);
const changeMsgFn = (arg) => {
dispatch({
type: "changeMsgFn",
value: arg,
});
};
return (
<Fragment>
<div>{msg}</div>
<button onClick={() => changeMsgFn("你好")}>修改msg</button>
<button onClick={() => changeMsgFn("再见")}>修改msg</button>
</Fragment>
);
}
export default App1;
此页面不支持夜间模式!
已进入夜间模式!
已进入普通模式!
搜索框不允许为空
签到成功!经验+5!芋圆币+2!
签到失败!今日已签到!
需要登录社区账号才可以进入!