项目作者: fefeng

项目描述 :
最简单的react、redux、webpack 整合实例,例子使用ES2015,建议提前学习。
高级语言: JavaScript
项目地址: git://github.com/fefeng/react-table.git
创建时间: 2016-02-03T05:18:17Z
项目社区:https://github.com/fefeng/react-table

开源协议:

下载


react+redux 实例

技术列表

运行实例

  1. 下载代码 git clone https://github.com/fengzier/react-table.git
  2. 安装依赖 npm install
  3. 运行项目 npm start
  4. 访问:http://127.0.0.1:8080/webpack-dev-server/

项目结构分析

  1. react-table/
  2. ├── components/ //react组件
  3. ├── R_Table.js
  4. ├── R_Tbody.js
  5. └── R_Toobar.js
  6. ├── app/ // 组件入口
  7. ├── app.js
  8. ├── app.scss
  9. └── common.js
  10. ├── redux/ //redux文件
  11. ├── actions.js
  12. ├── app.js
  13. └── reducers.js
  14. ├── index.html
  15. ├── package.json
  16. ├── webpack.config.js //webpack 配置文件

页面效果

img

代码分析

/webpack.config.js

  1. var webpack = require('webpack');
  2. module.exports = {
  3. entry: [
  4. 'webpack/hot/only-dev-server', //改变文件立刻刷新,而且保存了现有的state
  5. './app/app.js' //入口配置文件
  6. ],
  7. output: {
  8. path: './build',
  9. filename: "bundle.js" //输出文件,即index.html需要引入的文件。
  10. },
  11. module: {
  12. loaders: [
  13. { test: /\.js?$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/ },
  14. { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/},
  15. { test: /\.css$/, loader: "style!css" },
  16. { test: /\.scss$/, loader: 'style!css!sass'},
  17. ]
  18. },
  19. resolve:{
  20. extensions:['','.js','.json']
  21. },
  22. plugins: [
  23. new webpack.NoErrorsPlugin()
  24. ]
  25. };
  • $ webpack // 编译文件
  • $ webpack -w // 提供watch方法,实时进行打包更新
  • $ webpack -p // 对打包后的文件进行压缩,提供production
  • $ webpack -d // 提供source map,方便调试。

index.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>React Table</title>
  6. <link rel="stylesheet" type="text/css" href="./node_modules/bootstrap/dist/css/bootstrap.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <div id="root"></div>
  11. </div>
  12. <script src="bundle.js"></script> //只引入打包好的文件。
  13. </body>
  14. </html>

在react的开发理念中,HTML只是作为一个站点的入口。

/app/app.js

  1. import './app.scss'; //直接引入sass文件而不是css文件
  2. import React from 'react';
  3. import { render } from 'react-dom';
  4. import { createStore } from 'redux';
  5. import { Provider } from 'react-redux';
  6. import React_table_App from '../containers/App';
  7. import reactTableApp from '../containers/reducers';
  8. import common from './common';
  9. let store = createStore(reactTableApp); // Redux 应用只有一个单一的 store
  10. let data = common.getData(); // 随机生成数据
  11. render( //向页面输出组件
  12. <Provider store={store}> // store 只能使用privider传入
  13. <React_table_App data={data}></React_table_App>
  14. </Provider>,
  15. document.getElementById('root') //将组件渲染到#root dom中
  16. )
  17. store.subscribe(() => // 监听store的变化
  18. console.log("dispatch:",store.getState())
  19. );

./containers/actions.js

  1. export const FILTER = 'FILTER';
  2. export const LENGHTMENU = 'LENGHTMENU';
  3. export function doFilter(text) {
  4. return { type: FILTER, text }
  5. }
  6. export function lengthMenu(text) {
  7. return { type: LENGHTMENU, text }
  8. }

定义actions

./containers/reducers.js

  1. import { combineReducers } from 'redux';
  2. import { FILTER,LENGHTMENU } from './actions';
  3. function operation(state = [],action){
  4. switch(action.type){
  5. case FILTER:
  6. return {FILTER:action.text};
  7. case LENGHTMENU:
  8. return {LENGHTMENU:action.text};
  9. default:
  10. return state;
  11. }
  12. }
  13. const reactTableApp = combineReducers({ //使用combineReducers 组合多个reducer
  14. operation,
  15. })
  16. export default reactTableApp;

./containers/App.js

  1. import React, { Component, PropTypes } from 'react';
  2. import { connect } from 'react-redux';
  3. import { doFilter,lengthMenu } from './actions';
  4. import R_Table from '../components/R_Table';
  5. import R_Toobar from '../components/R_toobar';
  6. class App extends Component{
  7. render(){
  8. const {dispatch,operation} = this.props;
  9. return(
  10. <div>
  11. <R_Toobar
  12. onFilterChange={
  13. (text)=>{
  14. dispatch(doFilter(text));
  15. }
  16. }
  17. onLengthMenuChange={
  18. (text)=>{
  19. dispatch(lengthMenu(text));
  20. }
  21. }
  22. {...this.props}>
  23. </R_Toobar>
  24. <R_Table operation={operation} {...this.props}>< _Table>
  25. </div>
  26. )
  27. }
  28. }
  29. function select(state){
  30. return {
  31. operation: state.operation
  32. };
  33. }
  34. export default connect(select)(App);