react-redux 的使用
<h1 id="前言">前言</h1><p>最近在学 react,看到 react-redux 这里,刚开始觉得一脸懵逼,后面通过查阅相关资料和一些对源码的解释,总算有点头绪,今天在这里总结下。</p>
<p>类似于 vue,React 中组件之间的状态管理 第三方包为:react-redux。react-redux 其实是 Redux的官方React绑定库,它能够使你的React组件从Redux store中读取数据,并且向store分发actions以更新数据。</p>
<p>值得一提的是 redux 其实是一个第三方 数据状态管理的库,它不仅仅可以和react 结合使用,你也可以把它应用到 vue 中 , react-redux 其实是帮我们封装了 redux 连接 react 的一些操作,使用 react-redux 可以非常简单的在 react 中使用 redux 来管理我们应用的状态。</p>
<p> </p>
<h2 id="使用-redux-来管理-react-数据">使用 redux 来管理 react 数据</h2>
<h3 id="开始之前先安装">开始之前先安装</h3>
<pre><code class="hljs nginx">npm install redux react-redux --save</code></pre>
<p>安装完这两个库之后,可以用 redux 来创建 store , 利用 react-redux 获取 store 中的数据或者更新数据。</p>
<p>react-redux 提供了两个常用的 api ,一个是: Provider,一个是:connect。 组件之间共享的数据是 Provider 这个顶层组件通过 props 传递下去的,<strong>store必须作为参数放到Provider组件中去</strong>。而 connect 则提供了组件获取 store 中数据或者更新数据的接口。</p>
<p> </p>
<h3 id="创建-store">创建 store</h3>
<p>了解一些基本的概念之后,我们现在开始来用。</p>
<p>在外围顶层组件中引入 redux 和 react-redux 两个库。我们在做业务之前都需要将页面拆分成不同的组件,这里的外围组件通常指的是我们拆分后的所有组件的父组件。</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">import { createStore } <span class="hljs-keyword">from <span class="hljs-string">'redux'
<span class="hljs-keyword">import { Provider } <span class="hljs-keyword">from <span class="hljs-string">'react-redux'</span></span></span></span></span></span></code></pre>
<p>引入 createStore 来创建组件共享的数据,这个是 redux 中提供的一个方法,我们直接引入。</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">const</span> themeReducer = <span class="hljs-function">(<span class="hljs-params">state, action) => {
<span class="hljs-keyword">if (!state) <span class="hljs-keyword">return {
<span class="hljs-attr">themeColor: <span class="hljs-string">'red'
}
<span class="hljs-keyword">switch (action.type) {
<span class="hljs-keyword">case <span class="hljs-string">'CHANGE_COLOR':
<span class="hljs-keyword">return { ...state, <span class="hljs-attr">themeColor: action.themeColor }
<span class="hljs-keyword">default:
<span class="hljs-keyword">return state
}
}
<span class="hljs-keyword">const</span> store = createStore(themeReducer)</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>上面的代码创建了一个 {themeColor: 'red'} 的 store,并且提供了修改颜色的方法,组件通过指定的 action.type 中的 CHANGE_COLOR 字段来修改主体颜色。代码中可以看出,我们传入非法的修改字段名,则返回原始的 state,即修改失败。</p>
<p> </p>
<h3 id="使用-store-中的-state">使用 store 中的 state</h3>
<p>创建完数据之后,组件中怎样使用到全局的数据状态呢?请看下面:</p>
<p>在需要使用数据的组件中引入 react-redux</p>
<pre><code class="hljs javascript"><span class="hljs-keyword">import { connect } <span class="hljs-keyword">from <span class="hljs-string">'./react-redux'</span></span></span></code></pre>
<p>我们从 react-redux 中引入了 connect 这个方法。其实 connect 方法一共有4个参数,这里主要讲前两个。</p>
<pre><code class="hljs">connect(mapStateToProps, mapDispatchToProps)(MyComponent)</code></pre>
<p>mapStateToProps 字面含义是把state映射到props中去,意思就是把Redux中的数据映射到React中的props中去。</p>
<p>也就是说你React想把Redux中的哪些数据拿过来用。</p>
<pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Header <span class="hljs-keyword">extends <span class="hljs-title">Component {
<span class="hljs-keyword">static propTypes = {
<span class="hljs-attr">themeColor: PropTypes.string
}
render () {
<span class="hljs-keyword">return (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">h1 <span class="hljs-attr">style=<span class="hljs-string">{{ <span class="hljs-attr">color: <span class="hljs-attr">this.props.themeColor }}>React.js<span class="xml"> 小书<span class="hljs-tag"></<span class="hljs-name">h1>
)
}
}
<span class="hljs-keyword">const mapStateToProps = <span class="hljs-function">(<span class="hljs-params">state) => {
<span class="hljs-keyword">return {
<span class="hljs-attr">themeColor: state.themeColor
}
}
Header = connect(mapStateToProps)(Header)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>上面代码是拿到 Redux store 中 themeColor 数据, 这是我们前面自己创建的数据,然后组件通过 this.props.themeColor 调用。</p>
<p>那么这样就可以实现渲染,就是把Redux中的state变成React中的props。</p>
<p><span style="position: relative; left: -100000px">资源搜索网站大全 https://www.renrenfan.com.cn</span> <span style="position: relative; left: -100000px">广州VI设计公司https://www.houdianzi.com</span></p>
<h3 id="修改-store-中-state">修改 store 中 state</h3>
<p>现在的主题颜色是自己定义的红色,如果我们想在某个组件中修改这个全局的状态,比如修改为蓝色,该如何操作,这就涉及到修改 store 中 state。</p>
<p>修改 Redux 中的 state ,需要用到前面 connect 中的第二个参数:mapDispatchToProps,通过上面的分析,相信这个函数也很好理解,就是把各种 dispatch也变成了 props 让你可以直接使用,进而修改 store 中的数据。</p>
<pre><code class="hljs javascript"><span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">SwitchColor <span class="hljs-keyword">extends <span class="hljs-title">Component {
handleChangeColor (color) {
<span class="hljs-keyword">this.props.changeColor(color)
}
render() {
<span class="hljs-keyword">return (
<span class="xml"><<span class="xml"><span class="hljs-tag"><span class="hljs-name">div</span></span></span><span class="xml"><span class="hljs-tag">>
<span class="hljs-tag"><<span class="hljs-name">button <span class="hljs-attr">style=<span class="hljs-string">{{color: <span class="hljs-attr">this.props.themeColor}} <span class="hljs-attr">onClick=<span class="hljs-string">{this.handleChangeColor.bind(this, '<span class="hljs-attr">blue')}>blue<span class="hljs-tag"></<span class="hljs-name">button>
<span class="hljs-tag"><<span class="hljs-name">button <span class="hljs-attr">style=<span class="hljs-string">{{color: <span class="hljs-attr">this.props.themeColor}} <span class="hljs-attr">onClick=<span class="hljs-string">{this.handleChangeColor.bind(this, '<span class="hljs-attr">red')}>red<span class="hljs-tag"></<span class="hljs-name">button>
<span class="hljs-tag"></<span class="xml"><span class="hljs-tag"><span class="hljs-name">div</span></span></span><span class="xml">>
)
}
}
<span class="hljs-keyword">const mapStateToProps = <span class="hljs-function">(<span class="hljs-params">state) => {
<span class="hljs-keyword">return {
<span class="hljs-attr">themeColor: state.themeColor
}
}
<span class="hljs-keyword">const mapDispatchToProps = <span class="hljs-function">(<span class="hljs-params">dispatch) => {
<span class="hljs-keyword">return {
<span class="hljs-attr">changeColor: <span class="hljs-function">(<span class="hljs-params">color) => {
dispatch({<span class="hljs-attr">type: <span class="hljs-string">'CHANGE_COLOR', <span class="hljs-attr">themeColor: color})
}
}
}
SwitchColor = connect(mapStateToProps, mapDispatchToProps)(SwitchColor)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>上面的代码实现了通过点击按钮来修改主题颜色,我们在 mapDispatchToProps 中调用了 dispatch() 来通知 Redux store 修改 数据,这里需要注意传入 dispatch() 的参数为一对象,其中必须有 type 属性来告诉 store 修改哪些数据。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/qianxiaox/p/14120360.html
頁:
[1]