Bootstrap

1分钟带你入门Redux、React-Redux

前言:还在迷茫在Redux门外的小伙伴们,今天de福利就能让你豁然开朗。至于Redux的目的我在这里就不在累赘了,直接上手基本用法。

Redux

我们先来看看Redux简单的工作流程吧?上图:

import {createStore} from redux

function reducer(state,action) {
	... 修改state的地方
}

const store = createStore(reducer) // 第一步 创建数据仓库store,并定义好reducer

store.subscribe(() => { // 第二步 声明监听函数,当state发生变化的时候会触发
	... 
})

store.dispatch({type: xxx}) // 第三部 派发一个action触发reducer中对应的方法


简单的三部曲就是redux的全部,哈哈,如果不清楚,那就贴个完整的代码吧

import React from "react";
import { useState, useEffect } from "react";
import { createStore } from "redux";

function counter(count = 0, action) {
  switch (action.type) {
    case "INCREMENT":
      return count + 1;
    case "DECREMENT":
      return count - 1;
    default:
      return count;
  }
}
const store = createStore(counter);

export default () => {
  const [count, setCount] = useState(0);
  useEffect(() => {
    store.subscribe(() => {
      setCount(store.getState());
    });
  }, []);
  return (
    

{count}

); };

React-Redux

看到清楚上面的所展示Redux的"全部世界"之后,是不是感觉用起来还是不爽呀,那么React-Redux就是为了让你用来爽的,废话不多说,线上代码:

import React from "react";
import { useState, useEffect } from "react";
import { createStore } from "redux";
import {Provider,connect} from 'react-redux'

function counter(count = 0, action) {
 ... 这里同上略过
}
const store = createStore(counter);

export default () => {
	return (
  	 // 这里跟context的使用是不是很像呀? 一个统一的数据提供方
    	
<ButtonGroup /> </div> </Provider> ) } /** React-Reudx 提供了connect连接器(高阶函数)可以吧state和dispatch挂载到props上去 **/ // 向props挂载state const mapStateToProps = state => { return {count: state} } const Title = connect(mapStateToProps)((props) => { return <p>{props.count}</p> }) // 向props挂载dispatch const mapDispatchToProps = dispatch => { return { increment: () => dispatch({ type: "INCREMENT" }), decrement: () => dispatch({ type: "DECREMENT" }) } } const ButtonGroup = connect(null, mapDispatchToProps)((props) => { return <div> <button onClick={() => props.increment()}>-</button> <button onClick={() => props.increment()}>+</button> </div> })</code></pre><p data-type="paragraph">好了,我们来总结一下:</p><ul data-type="bulletedlist"><li><p data-type="paragraph">React-Redux 提供了两个最重要的API,一个是Provider,一个是connect</p></li><li><p data-type="paragraph">Provider数据提供方,类似React Context (如何不清楚的小伙伴可以参考<a data-type="link" href="#"></a>)</p></li><li><p data-type="paragraph">connect连接器,就是一个高阶函数;作用有二,一是能够获取Provider中store中的公共数据,二就是派发dispatch,并且都是从props上获取;这样就可以让你在用父子组件通信的成本,实现任意组件之间的通信</p></li></ul><p data-type="paragraph"></p><h2 data-type="heading">小结</h2><ul data-type="bulletedlist"><li><p data-type="paragraph">Redux主要就是实现单向数据流的功能</p></li><li><p data-type="paragraph">首先,createStore,并把reducer函数传递给他</p></li><li><p data-type="paragraph">然后,将createStore创建的store进行监听订阅,store.subscribe</p></li><li><p data-type="paragraph">最后,通过store.dispatch指定需要的action,从而触发之前在store.subscribe中定义的函数</p></li><li><p data-type="paragraph">还有就是通过store.getState获取store中定义公共数据</p></li><li><p data-type="paragraph">React-Redux主要就是让Redux能够愉快在React中使用</p></li><li><p data-type="paragraph">Proivder一个高阶组件,简化了store.sbuscribe和store.getState的方式</p></li><li><p data-type="paragraph">connect是一个高阶组件, 方便使用state和dispatch(如果对高阶组件不了解的,可以先看看这个<a data-type="link" href="#"></a></p></li></ul><p data-type="paragraph"></p><blockquote data-type="blockquote"><p data-type="paragraph">最后,希望你能在React中愉快的使用Redux啦! 当然更多信息请参考官方文档。</p><p data-type="paragraph">Redux除了上文介绍的这些,还有个中间件的概念需要了解一下<a data-type="link" href="#"></a></p></blockquote><h2 data-type="heading">参考:</h2><ul data-type="bulletedlist"><li><p data-type="paragraph"><a data-type="link" href="#"></a></p></li><li><p data-type="paragraph"><a data-type="link" href="#"></a></p></li></ul></div></div> </article> <nav class="blog-pagination" aria-label="Pagination"> <a class="btn btn-outline-primary" href="#">Older</a> <a class="btn btn-outline-secondary disabled">Newer</a> </nav> </div> <div class="col-md-4"> <div class="position-sticky" style="top: 2rem;"> <div class="p-4 mb-3 bg-light rounded"> <h4 class="fst-italic">关于我们</h4> <p class="mb-0">Customize this section to tell your visitors a little bit about your publication, writers, content, or something else entirely. Totally up to you.</p> </div> <div class="p-4"> <h4 class="fst-italic">热门标签</h4> <a href="/cms/page/tag/9198" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">钉钉开放平台</button> </a><a href="/cms/page/tag/9202" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Javaspring</button> </a><a href="/cms/page/tag/9203" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">字节offer</button> </a><a href="/cms/page/tag/9204" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">小程序管理平台</button> </a><a href="/cms/page/tag/9206" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">线下办公</button> </a><a href="/cms/page/tag/9207" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">主机</button> </a><a href="/cms/page/tag/9209" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">居家办公</button> </a><a href="/cms/page/tag/9212" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">钉钉应用开发</button> </a><a href="/cms/page/tag/9213" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">连接器</button> </a><a href="/cms/page/tag/9214" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Redis 消费队列</button> </a><a href="/cms/page/tag/9217" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">bootstrap_lite</button> </a><a href="/cms/page/tag/4" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">读书笔记</button> </a><a href="/cms/page/tag/5" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">算法</button> </a><a href="/cms/page/tag/7" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">学习方法</button> </a><a href="/cms/page/tag/11" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">大数据</button> </a><a href="/cms/page/tag/9" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Python</button> </a><a href="/cms/page/tag/8" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">高效工作</button> </a><a href="/cms/page/tag/15" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">开源</button> </a><a href="/cms/page/tag/14" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">深度思考</button> </a><a href="/cms/page/tag/13" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">团队管理</button> </a><a href="/cms/page/tag/10" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">前端</button> </a><a href="/cms/page/tag/12" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">程序员</button> </a><a href="/cms/page/tag/16" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">redis</button> </a><a href="/cms/page/tag/26" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">数据库</button> </a><a href="/cms/page/tag/20" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Docker</button> </a><a href="/cms/page/tag/21" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">个人成长</button> </a><a href="/cms/page/tag/27" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">nginx</button> </a><a href="/cms/page/tag/18" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Linux</button> </a><a href="/cms/page/tag/17" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">微服务架构</button> </a><a href="/cms/page/tag/23" class="link-secondary m-0 m-md-0 p-1" target="_blank"> <button class="btn text-start text-secondary text-nowrap" style="width: 106px">Java并发</button> </a> </div> <div class="p-4"> <h4 class="fst-italic">Elsewhere</h4> <ol class="list-unstyled"> <li><a href="https://www.github.com">GitHub</a></li> <li><a href="/cms/page/index">竹心导航</a></li> </ol> <p> <a href="#">返回顶部</a> </p> </div> </div> </div> </div> </main> <footer class="blog-footer"> <p> <a href="/cms/page/index">博客系统</a> by <a href="/cms/page/index">i230.com</a>.</p> <p><p>©CopyRight 2020- <span id="year">{{year}}</span> I230.COM Inc All Rights Reserved </p> </footer> </div> <script src="/static/assets/dist/js/bootstrap.bundle.min.js"></script> <script src="/static/assets/js/vue.min.js"></script> <script type="application/javascript"> var app = new Vue({ el: '#app', data: { message: 'Hello Vue!', year: (new Date()).getUTCFullYear(), searchString: "" }, methods: { openSearch: function (searchEngineUrl) { open(searchEngineUrl + this.searchString, "_blank") }, searchBaidu: function (event) { this.openSearch("https://www.baidu.com/s?wd=") }, searchBingInternal: function () { this.openSearch("https://cn.bing.com/search?ensearch=0&q=") }, searchBing: function () { this.openSearch("https://cn.bing.com/search?ensearch=1&q=") }, searchGoogle: function () { this.openSearch("https://www.sogou.com/web?query=") }, searchSougou: function () { this.openSearch("https://www.sogou.com/web?query=") }, search360: function () { this.openSearch("https://www.so.com/s?q=") }, searchTaobao: function () { this.openSearch("https://s.taobao.com/search?q=") } } }) hljs.highlightAll(); </script> </body> </html>