gpt4 book ai didi

reactjs - 用于使用react-redux创建react-app的HMR

转载 作者:行者123 更新时间:2023-12-03 14:09:42 24 4
gpt4 key购买 nike

使用 create-react-app 初始化的应用程序未启用 HMR 。这里有一篇关于如何启用它的博客文章:http://chrisshepherd.me/posts/adding-hot-module-reloading-to-create-react-app

ReactDOM.render(
<App />,
rootEl
);

if (module.hot) {
module.hot.accept('./App', () => {
const NextApp = require('./App').default;
ReactDOM.render(
<NextApp />,
rootEl
);
});
}

我正在尝试做类似的事情,尽管我添加了 reduxreact-router-redux混合:

App.js

import React from 'react'
import { Provider } from 'react-redux'
import store from './store/store'
import routes from './routes'

const App = (
<Provider store={store}>
{ routes }
</Provider>
);

export default App;

routes.js

import React from 'react';
import { browserHistory, Router, Route } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import store from '../store/store';
import { AnonymousLayout } from '../layouts';
import { LoginForm } from './Login';

const history = syncHistoryWithStore(browserHistory, store);

export default (
<Router history={history}>
<Route path="/" component={AnonymousLayout}>
<Route path="/login" component={LoginForm} />
</Route>
</Router>
);

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import App from './client/App';

const rootEl = document.getElementById('root');

ReactDOM.render(
App,
rootEl
);

if (module.hot) {
module.hot.accept('./client/App', () => {
const NextApp = './client/App';
ReactDOM.render(
<NextApp />,
rootEl
);
});
}

但是,我刚刚收到此错误:

Warning: [react-router] You cannot change <Router routes>; it will be ignored

有什么方法可以将 HMR 侵入这个项目吗?

最佳答案

丹·阿布拉莫夫 posted a solution这适用于我的情况:

index.js

// regular imports
ReactDOM.render(<App /> , document.getElementById('root'))

if (module.hot) {
module.hot.accept('./App', () => {
ReactDOM.render(<App />, document.getElementById('root'))
})
}

store.js

import { createStore } from 'redux'

import rootReducer from './reducers'

const configureStore = () => {
const store = createStore(rootReducer)

if (process.env.NODE_ENV !== "production") {
if (module.hot) {
module.hot.accept('./reducers', () => {
store.replaceReducer(rootReducer)
})
}
}

return store
}

export default configureStore

关于reactjs - 用于使用react-redux创建react-app的HMR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com