React redux [?ri?d?ks]
〇、预备
安装
npm install --save redux
npm install --save react-redux(基础款用不上)
npm install --save redux-thunk
npm install --save-dev redux-devtools(不必要)
一、基础版
已经将reducer和actions抽出
1.仓库文件
import { createStore, combineReducers, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import userdata from "./userData/userData";
let rootReducer = combineReducers({ userdata });
export default createStore(rootReducer, applyMiddleware(thunk));
2.reducer文件
let initUserData = {}
export default (userdata = initUserData, action) => {
const { type, payload } = action
switch (type) {
case "INITUSERDATA": return { ...payload };
}
return userdata
}
3.actions文件
export const inituserdatd = payload => ({
type: "INITUSERDATA",
payload
})
4.修改数据的组件
import store from "../../store/index.js"
import { inituserdatd } from "../../store/userData/actions"
export default class EPLogin extends React.Component {
store.getState().UserData
store.dispatch(inituserdatd({ ...data }));
}
5.根文件
import store from "../src/store/index";
ReactDOM.render(
<>
<BrowserRouter>
{}
<App />
{}
</BrowserRouter>
</>,
document.getElementById('root')
);
store.subscribe(render)
6.使用数据的文件
import store from "../../store/index.js"
export default class Over extends React.Component {
componentDidMount() {
console.log(store.getState().userdata);
}
}
二、react-redux强化版
仓库文件、reducer文件、actions文件不变,只是在使用和更改数据的组件稍有不一样,主要是简化数据修改和订阅的过程。
思路:
- 用Provider包裹最顶层的组件,提供一个store属性。这样redux任何组件里都可以使用store了。
- 使用connect()函数来链接react的组件和redux的store。记住:connect不能单独使用,必须要有Provider。
1.根文件
import { Provider } from "react-redux";
import store from "../src/store/index";
ReactDOM.render(
<>
{}
<Provider store={store}>
<App />
</Provider>
</>,
document.getElementById('root')
);
2.获取数据的组件
import { connect } from "react-redux"
class Over extends React.Component {
componentDidMount() {
console.log( this.props.userdata);
}
}
const mapStateToProps = (state) => ({
userdata: state.userdata
})
export default connect(mapStateToProps)(Over)
3.获取数据,同时修改数据的组件
import { inituserdatd } from "../../store/userData/actions"
import { connect } from "react-redux"
class EPLogin extends React.Component {
componentDidMount() {
this.props.adduserdata({ ...data })
console.log( this.props.userdata);
}
}
const mapStateToProps = (state) => ({
userdata: state.userdata
})
const mapDispatchToProps = (dispatch) => ({
adduserdata: (payload) => dispatch(inituserdatd(payload))
})
export default connect(mapStateToProps, mapDispatchToProps)(EPLogin)
三、在redux中异步获取数据
思路:在需要数据的组件渲染完成后调用dispatch中的方法,给store一个时机去发送请求,(如果发送请求的代码直接写在store里运行,就会导致需要使用数据的组件会在数据请求回来之前已经完成渲染),在store内请求数据,然后通过actions提交保存给自己,其他组件就可以从组件中store获取。
1.reducer文件
export default (meetcount = 0, action) => {
const { type, payload } = action
switch (type) {
case "CHANGEMEETCOUNT": {
meetcount = payload
} return meetcount;
}
return meetcount
}
2.actions文件
import { queryMeetdb } from "../../api/meets";
export const changemeetcount = (payload) => (async dispatch => {
let count = await queryMeetdb(payload);
let meetcount = count.data.length
dispatch({
type: "CHANGEMEETCOUNT",
payload: meetcount
});
})
3.使用数据的组件
import { connect } from "react-redux"
import { inituserdatd } from "../../store/userData/actions"
import { changemeetcount } from "../../store/meetCount/actions"
const AddMeeting = (props) => {
let epnameuse = props.userdata.epname
props.meetcountchange({ "meetuser": epnameuse })
}
const mapStateToProps = (state) => ({
userdata: state.userdata,
meetcount: state.meetcount
})
const mapDispatchToProps = dispatch => ({
meetcountchange: (payload) => dispatch(changemeetcount(payload)),
adduserdata: (payload) => dispatch(inituserdatd(payload))
})
export default connect(mapStateToProps, mapDispatchToProps)(AddMeeting)
优化内存可以使用Immutable 详情见后续
感谢你能看完,也感谢我能写完
|