在webpack应用的入口代码中,实现react相关组件的热重载(hot reload)
2017-04-02 00:36
846 查看
以下是应用的入口文件(main.jsx)的内容,其实现了react组件和reducer的hot reload。
import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { createStore, applyMiddleware, combineReducers } from 'redux'; import thunk from 'redux-thunk'; import { Router, hashHistory } from 'react-router'; import { syncHistoryWithStore, routerReducer } from 'react-router-redux' const getRootReducer = () => { var configObj = require( './redux/rootReducer.jsx' ).ReducerConfig return combineReducers( Object.assign( {}, configObj, // 补充处理routing信息的reducer { routing: routerReducer } ) ) }; const store = createStore( getRootReducer(), // 初始state { "async-items": { items: [] } }, applyMiddleware( thunk ) ); //Create an enhanced history that syncs navigation events with the store const history = syncHistoryWithStore( hashHistory, store ); //每次热重载,使用新的Router对象 let RouterKey = 0; const renderApp = () => { var routes = require( './app.jsx' ).RoutesConfig; var provider = ( <Provider store={store}> <Router key={RouterKey} history={history} routes={routes} /> </Provider> ); var AppContainer = null; try { // 生产环境下,使用IgnorePlugin排除了对react-hot-loader代码的引入 AppContainer = require( 'react-hot-loader' ).AppContainer; console.log( 'has AppContainer' ); } catch ( e ) { } ReactDOM.render( AppContainer ? <AppContainer> {provider} </AppContainer> : provider, document.getElementById( 'root' ) ); }; if ( module.hot ) { console.log( 'module.hot enabled' ); /* * 热重载reducer * rootReducer.jsx返回的是combineReducers的参数对象 * rootReducer.jsx是所有reducer的引用的入口 */ module.hot.accept( './redux/rootReducer.jsx', () => { store.replaceReducer( getRootReducer() ); }); /* * 热重载组件 * app.jsx返回的是Router的配置对象 * app.jsx是所有react组件的引用的入口,react组件又引用了action */ module.hot.accept( './app.jsx', () => { RouterKey++; renderApp(); }) } renderApp();
相关文章推荐
- 用 webpack 构建 node 后端代码,使其支持 js 新特性并实现热重载
- webpack+react-router实现代码拆分按需加载(上)
- React多页面应用5(webpack生产环境配置,包括压缩js代码,图片转码等)
- webpack+react-router实现代码拆分按需加载(下)
- React配合Webpack实现代码分割与异步加载
- React多页面应用4(webpack自动化生成多入口页面)
- Spring集成React用来开发前端----maven项目中用webpack打包react相关组件
- 使用React实现轮播效果组件示例代码
- 实现应用WebView组件加载使用HTML代码添加的帮助信息
- 巧用FileSystem组件实现WEB应用中的本地特定打印
- webpack react-hot-loader配置
- Vue+webpack+node.js实现价格监测应用Ponitor
- react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
- webpack 打包一个简单react组件
- react及webpack相关注意点,小工具
- ReactJS webpack实现JS模块化使用的坑
- react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware
- Webpack 实现 Node.js 代码热替换
- Webpack 实现 Node.js 代码热替换