博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B站 React教程笔记day2(3)React-Redux
阅读量:4312 次
发布时间:2019-06-06

本文共 2881 字,大约阅读时间需要 9 分钟。

刚才我们学习了Redux,大致步骤:

  1. 设置一个reducer,

  2. 弄一个store,从Redux.createStore(reducer);

  3. 弄一个render函数

  4. 注册一下render,store.subscibe(render)

  5. 写监听了,此时要记得store.dispatch(action),不是直接改store。

此时和React还没有直接产生关系,换句话说在React中没有使用Redux技术。

可看官方counter案例

 

结合官方文档看官方demo

React-Redux给我们提供了两个东西:Provider组件、connect函数。

Provider组件要求是最大的组件,传入store属性,此时天下无人不识君。

 

官方文档:

index.js:

import React from 'react'import { render } from 'react-dom'import { createStore } from 'redux'import { Provider } from 'react-redux'import App from './components/App'import reducer from './reducers'import 'todomvc-app-css/index.css'const store = createStore(reducer)render(    // 全局组件,传入store属性  
, document.getElementById('root'))

 

Provider自定义组件:

Makes the Redux store available to the connect() calls in the component hierarchy below。

这个Provider组件使得它内部的自定义组件可以使用connect()函数。

Normally, you can’t use connect() without wrapping a parent or ancestor component in <Provider>

通常的,你不能在没有Provider父亲或者组件的情况下,使用connect()函数。

属性store (Redux Store): APP中唯一的那个store

 

App.js:

import React from 'react'import Header from '../containers/Header'import MainSection from '../containers/MainSection'const App = () => (  
)export default App

 

Connects a React component to a Redux store.

将React组件和Redux的store进行连接。

 

connect providing a convenient API for the most common use cases.

connect提供了一个方便的API能够适应绝大多数工作。

 

It does not modify the component class passed to it; instead, it returns a new, connected component class for you to use

它没有更改你传进来的类,返回会返回一个已经连接好的新类。

 

第一个参数:mapStateToProps

If this argument is specified, the new component will subscribe to Redux store updates.

如果你传入了第一个参数,此时这个组件将注册Redux的store的更新信息。

 

This means that any time the store is updated, mapStateToProps will be called.

这意味着无论任何时候store被更改了,mapStateToProps函数将会被调用。

 

The results of mapStateToProps must be a plain object, which will be merged into the component’s props.

mapStateToProps 的返回值必须是一个纯JSON!这个JSON将与组件的props融合。也就是说,这个返回的JSON中的key将自动成为组件的props中的成员。

 

If you don't want to subscribe to store updates, pass null or undefined in place of mapStateToProps.

如果你不想订阅store的更新,此时不要传这个参数就行了,此时用null占一个位置即可。

 

export default connect(  null,  mapDispatchToProps)(App)

 

第2个参数,mapDispatchToProps

If a function is passed, it will be given dispatch.

如果第往connect函数中传入了第二个参数,且是一个函数,那么这个函数将获得dispatch方法,这可是可以号令action发出的方法啊!可以间接导致stage的改变。

 

It’s up to you to return an object that somehow uses dispatch to bind action creators in your own way.

返回一个对象如何绑定action creator(返回action的函数,就是action creator)取决于你自己

 

Tip: you may use the bindActionCreators() helper from Redux.

小提示:你可以使用bindActionCreators()方法轻松的将action creator接口和dispatch进行绑定。

 

If you omit it, the default implementation just injects dispatch into your component’s props.

如果你省略了第二个参数,此时系统还是会将dispatch对象注入到你的组件中,但是不好用,因为你看不见action清单,所以还是需要用bindActionCreators()去处理一下。

 

转载于:https://www.cnblogs.com/houfee/p/10931954.html

你可能感兴趣的文章
蜕变成蝶~Linux设备驱动之异步通知和异步I/O
查看>>
jquery简单开始
查看>>
作业2
查看>>
ios上架报错90080,90087,90209,90125 解决办法
查看>>
给button添加UAC的小盾牌图标
查看>>
如何退出 vim
查看>>
Robberies
查看>>
get post 提交
查看>>
R安装
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
Sultan's Dowry Problem - 苏丹新娘问题
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>