博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最简单的React和Redux整合的例子
阅读量:2389 次
发布时间:2019-05-10

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

安装create-react-app
npm install -g create-react-app
创建项目
create-react-app reactreduxrouterdemo
cd reactreduxrouterdemo
安装第三方模块
npm install --save redux
npm install --save react-redux
npm install --save react-router
修改index.js
import React, { Component } from 'react';import ReactDOM from 'react-dom';import { createStore } from 'redux';import { Provider, connect } from 'react-redux';//定义组件class App extends Component{    render() {        const {text, onChangeText, onButtonClick} = this.props;        return (            

{text}

); }}//actionconst changeTextAction = { type:'CHANGE_TEXT'}const buttonClickAction = { type:'BUTTON_CLICK'}//reducerconst initialState = { text: 'Hello'}const reducer = (state = initialState, action) => { switch (action.type) { case 'CHANGE_TEXT': return { text: state.text=='Hello' ? 'world':'Hello' } case 'BUTTON_CLICK': return { text: 'Hello world' } default: return initialState; }}//storelet store = createStore(reducer);//映射Redux state到组件的属性function mapStateToProps(state) { return { text: state.text }}//映射Redux actions到组件的属性function mapDispatchToProps(dispatch){ return{ onButtonClick:()=>dispatch(buttonClickAction), onChangeText:()=>dispatch(changeTextAction) }}//连接组件App = connect(mapStateToProps, mapDispatchToProps)(App)//渲染组件ReactDOM.render(
, document.getElementById('root'))
这个例子大概是最简单的了, 而且每一步注释都非常清楚了。
运行工程npm start
源代码工程
https://github.com/chenhaifeng2016/reactreduxrouter
 
 
你可能感兴趣的文章
outlook 2010 突破附件大小限制
查看>>
[转][Magick++] How to convert jpg image to raw 32 bit float
查看>>
[转]数据类型 -- uint32_t 类型
查看>>
[转]C语言系统资源控制(getrlimit && setrlimit)
查看>>
[转]linux文件系统基础知识
查看>>
[转]Centos5 下安装/配置lvm使用reiserfs文件系统
查看>>
[转]Use ReiserFS in CentOS 5(lvm)
查看>>
[转]KFS的部署与简单使用
查看>>
[转]KFS官方部署手册
查看>>
[转]Ubuntu 10.04 LTS 安装 sun-java6-jdk
查看>>
[转]mmap详解
查看>>
[转]HDFS和KFS 比较
查看>>
10 个令人惊喜的 jQuery 插件推荐
查看>>
Open Source GIS and Freeware GIS Applications
查看>>
Open Source GIS
查看>>
开源GIS软件SharpMap
查看>>
四个开源商业智能平台比较 (一)
查看>>
WinEdt如何使用中文
查看>>
Programmatic PlyQL via HTTP, ODBC, and JDBC
查看>>
Jackson 处理复杂类型(List,map)两种方法
查看>>