最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

如何使用react-redux插件

来源:动视网 责编:小采 时间:2020-11-27 19:41:41
文档

如何使用react-redux插件

如何使用react-redux插件:这次给大家带来如何使用react-redux插件,使用react-redux插件的注意事项有哪些,下面就是实战案例,一起来看一下。react-redux简介react-redux是使用redux开发react时使用的一个插件,另外插一句,redux不是react的产品,vue和angular中
推荐度:
导读如何使用react-redux插件:这次给大家带来如何使用react-redux插件,使用react-redux插件的注意事项有哪些,下面就是实战案例,一起来看一下。react-redux简介react-redux是使用redux开发react时使用的一个插件,另外插一句,redux不是react的产品,vue和angular中


这次给大家带来如何使用react-redux插件,使用react-redux插件的注意事项有哪些,下面就是实战案例,一起来看一下。

react-redux简介

react-redux是使用redux开发react时使用的一个插件,另外插一句,redux不是react的产品,vue和angular中也可以使用redux;下面简单讲解,如何使用react-redux来开发react。

描述

这个插件可以让我们的redux代码更加的简洁和美观。我假设你已经有一个使用create-react-app创建的一个可以显示hello world的react环境,并且已经安装来redux。

注意:如果是刚使用create-react-app创建的,需要运行 npm run eject弹出个性化设置,这样就可以自定义配置了。

安装

npm i react-redux --save

使用

react-redux提供了两个重要的接口:Provider、connect,使用了这个插件,react-redux的subscribe和dispatch就可以忘记来,它们就用不着了。

代码结构

//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { reducer } from './index.redux';
import { Provider } from 'react-redux'
const store = createStore(reducer, applyMiddleware(thunk));
ReactDOM.render(
 <Provider store={store}>
 <App />
 </Provider>,
 document.getElementById('root')
);
registerServiceWorker();

Provider中应用的最外层,把store传给它,只使用这一次。

//app.js
import React, { Component } from 'react';
import { addCreator, removeCreator, addAsync } from './index.redux';
import { connect } from 'react-redux';
class App extends Component {
 render() {
 return (
 <p className="App">
 <h1>现在有美女{this.props.num}个。</h1>
 <button onClick={this.props.addCreator}>add</button>
 <button onClick={this.props.removeCreator}>remove</button>
 <button onClick={this.props.addAsync}>addAsync</button>
 </p >
 );
 }
}
//定义把state中哪个属性放到props中
 function mapStateToProps(state) {
 return { num: state }
 }
 //定义把哪些方法放到props中
 const actionCreators={ addCreator, removeCreator, addAsync };
 //connect其实就是一个高阶组件,把app传进去,返回一个新的app组件
 App = connect(mapStateToProps, actionCreators)(App);
export default App;

connect负责从外部获取组件需要的参数。通过connect定义后,放到props中的属性和方法就可以直接通过this.props来获取。

下面是reducer的定义。

//react.redux.js
const Add = 'addGirl', Remove = "removeGirl";
export function reducer(state = 0, action) {
 switch (action.type) {
 case Add:
 return state + 1;
 case Remove:
 return state - 1;
 default:
 return 10;
 }
}
export function addCreator() {
 return { type: Add };
}
export function removeCreator() {
 return { type: Remove };
}
export function addAsync() {
 return (dispatch, getState) => {
 setTimeout(function () {
 dispatch(addCreator());
 }, 1000);
 }
}

使用装饰器的方式更优雅的写Conect

首先需要安装babel-plugin-transform-decorators-legacy,并在package.json中配置。

安装

npm i babel-plugin-transform-decorators-legacy --save-dev 这个只是开发使用,所以安装到--save-dev

配置

配置babel的plugins属性

 "babel": {
 "presets": [
 "react-app"
 ],
 "plugins": [
 ["transform-decorators-legacy"]
 ]
 }

修改原来写法
使用@connect来重新定义,写到class的上头即可。

//App.js
@connect((state) => ({ num: state }),{ addCreator, removeCreator, addAsync })
class App extends Component {
.....//省略
// function mapStateToProps(state) {
// return { num: state }
// }
// App = connect(mapStateToProps, { addCreator, removeCreator, addAsync })(App);

VS Code 装饰器提示“experimentalDecorators”的解决办法

点击Visual Studio Code左下角的配置按钮(或者文件>首选项>配置,Windows环境),打开用户设置窗口,在搜索框内输入“experimentalDecorators”,发现竟然能够找到选项,如下:

"javascript.implicitProjectConfig.experimentalDecorators": false

改成true就可以了。

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

怎样使用Vue实现倒计时按钮

怎样利用Vue写一个双向数据绑定

文档

如何使用react-redux插件

如何使用react-redux插件:这次给大家带来如何使用react-redux插件,使用react-redux插件的注意事项有哪些,下面就是实战案例,一起来看一下。react-redux简介react-redux是使用redux开发react时使用的一个插件,另外插一句,redux不是react的产品,vue和angular中
推荐度:
标签: 如何 插件 React
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top