React
reduc 是(React全家桶)的核心成员
新建项目怎么使用 redux和react-redux?
import {createStore,combineReducers,applyMiddleware} from 'redux'
import reduxThunk from 'redux-thunk' // 用于使用异步操作的插件
import {colorReducer,countReducer} from './reducers.js'
let rootReducer = combineReducers({
colorReducer,countReducer
});
let store = createStore(rootReducer,applyMiddleware(reduxThunk));
// export default {store}
// 使用方想要通过解构的方式 获取store 是不支持的
export default store
import * as types from './action-types.js'
// 纯函数
export function colorReducer(state,action){
if(!state){
return{
color:'blue'
}
}
switch(action.type){
case types.COLOR:
return{
...state,
color:action.color
}
break;
default:
return state
break;
}
}
export function countReducer(state,action){
if(!state){
return{
count:0
}
}
switch(action.type){
case types.COUNT:
return{
...state,
coUNT:action.count
}
break;
default:
return state
break;
}
}
changeColor
,changeColor11
方法是由于用户操作触发执行,从而实现数据更新。import {COLOR} from './action-types.js'
// import store from '../store/index.js'
export const changeColor = (str)=>{
return{
type:COLOR,
color:str
}
}
<-- 这个函数体里面我们用来进行对异步代码的编写 -->
export const changeColor11 = (str)=>{
return function(dispatch,getState){
<-- 这里的 dispatch,getState就是 store 里面对应的两个函数 -->
console.log(dispatch,getState);
// console.log(store.getState());
setTimeout(()=>{
dispatch({
type:COLOR,
color:str
})
},2000);
}
}
import {COLOR} from './action-types.js'
,在 actions.js文件中,可以用action-types.js文件中创造的属性依据 type作为属性值,来判断在函数方法中需要数据更新的属性import * as types from './action-types.js
,在 reducers.js纯函数文件中,可以用 action-types.js文件中创造的属性依据 type作为属性值,来判断在函数方法中需要数据更新的属性export const COLOR = 'CHANGE_COLOR'
export const COUNT = 'CHANGE_COUNT'
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'
import store from '../store/index.js'
import Header from './header.js'
import Content from './content.js'
import Content2 from './content.js'
class App extends React.Component {
constructor() {
super();
}
render() {
return (
{/* 是把 store赋值给了Provide的context,
使其所有的子孙组件都可以从其中获取数据 */}
)
}
}
export default App
Hreder
,Content
this.props
则可以到获取 store中的 state数据,同时需要使用connect(redux-react)处理当前组件,import React from 'react';
import ReactDOM from 'react-dom';
import {connect} from 'react-redux'
class Header extends React.Component {
constructor() {
super();
}
render() {
return
头部
;
}
}
<-- 通过 connect方法,子组件封装 -->
Header = connect((state)=>{
return{
color:state.colorReducer.color
}
},(dispatch)=>{
return{
dispatch
}
})(Header)
export default Header
import React from 'react';
import {connect} from 'react-redux';
import {changeColor,changeColor11} from '../store/actions.js'
import store from '../store/index.js';
class Content extends React.Component {
constructor() {
super();
}
fn(str){
this.props.dispatch(changeColor11(str));
}
render() {
return
这是内容
;
}
}
<-- 通过 connect方法,子组件封装 -->
Content = connect((state)=>{
return{
color:state.colorReducer.color,
}
},(dispatch)=>{
return{
dispatch
}
})(Content)
export default Content
(dispatch)=>{return{dispatch}}
变为{changeColor:changeColor,changeColor11:changeColor11}
这样写可以使点击事件执行触发函数方法fn
,看上去更方便,然而却不利于理解。import React from 'react';
import ReactDOM from 'react-dom';
import {connect} from 'react-redux';
import {changeColor,changeColor11} from '../store/actions.js'
import store from '../store/index.js';
class Content2 extends React.Component {
constructor() {
super();
}
fn(str){
this.props.changeColor11(str);
}
render() {
return
这是内容
;
}
}
<-- 通过 connect方法,子组件封装 -->
Content2 = connect((state)=>{
return{
color:state.colorReducer.color,
}
},{
changeColor,
changeColor11
})(Content2)
export default Content2