需要帮助配置react-hot-loader

时间:2017-10-31 03:42:12

标签: reactjs typescript react-hot-loader hot-module-replacement

我使用最新的ASP.NET 2.0 react SPA模板,该模板是使用命令 dotnet new react创建的。它开箱即用,热模块更换(代码编辑自动更新浏览器)。

然后我想添加一个名为 App 的顶级应用容器来保存应用状态,如本博客文章所述: react without redux article。应该简单吧?请注意,该文章使用的是javascript,但我使用的是Typescript,因为这就是模板的设计方式。但是在添加一个简单的顶级容器组件并更新热加载程序配置后,它不起作用。我收到一个错误,该组件不知道如何重新加载自己。这是正在运行的代码(来自模板):

import './styles/site.scss';
import 'bootstrap';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { BrowserRouter } from 'react-router-dom';
import * as RoutesModule from './routes';
let routes = RoutesModule.routes;

function renderApp() {
    // This code starts up the React app when it runs in a browser. It sets up the routing
    // configuration and injects the app into a DOM element.
    const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
    ReactDOM.render(
        <AppContainer>
            <BrowserRouter children={ routes } basename={ baseUrl } />
        </AppContainer>,
        document.getElementById('react-app')
    );
}

renderApp();

// Allow Hot Module Replacement
if (module.hot) {
    module.hot.accept('./routes', () => {
        routes = require<typeof RoutesModule>('./routes').routes;
        renderApp();
    });
}

这是我更改后的代码,它不起作用:

import './styles/site.scss';
import 'bootstrap';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { BrowserRouter } from 'react-router-dom';
import * as RoutesModule from './routes';
import  App  from './components/App';
import * as AppModule from './components/App'

let routes = RoutesModule.routes;

const render = (Component: any) => {
    // This code starts up the React app when it runs in a browser. It sets up the routing
    // configuration and injects the app into a DOM element.
    //const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
    ReactDOM.render(
        <AppContainer>
            <Component />
        </AppContainer>,
        document.getElementById('react-app')
    );
}

render(App);

// Allow Hot Module Replacement
if (module.hot) {
    module.hot.accept('./components/App', () => {
        const nextRoot = (require('./components/App') as typeof AppModule).default;
        render(nextRoot);
    });
}

供参考,这里是./routes文件(我没有更改此文件):

import * as React from 'react';
import { Route } from 'react-router-dom';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';

export const routes = <Layout>
    <Route exact path='/' component={ Home } />
    <Route path='/counter' component={ Counter } />
    <Route path='/fetchdata' component={ FetchData } />
</Layout>;

这是我的新App容器组件:

import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import { BrowserRouter } from 'react-router-dom';
import * as RoutesModule from '../routes';
import { ReactElement } from "react";
let routes = RoutesModule.routes;

interface AppState {
    stateDescription: string;
}

export default class App extends React.Component<{}, {}> {
    constructor(props: any) {
        super(props);
        this.state = { stateDescription: 'My Cool App State' };
    }

    public render() {
        const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href')!;
        return (
            <BrowserRouter children = { routes } basename = { baseUrl } />
        );
    }
}

非常感谢任何想法或建议!

1 个答案:

答案 0 :(得分:0)

我能够通过将我的boot.tsx文件恢复为原始模板版本,并将我的App组件放在routes.tsx文件中来解决此问题。你可以看到我刚刚在新的App元素中包装了所有内容。这是新的routes.tsx文件:

import * as React from 'react';
import { Route } from 'react-router-dom';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { App } from './components/App';

export const routes = <App>
  <Layout>
    <Route exact path='/' component={ Home } />
    <Route path='/counter' component={ Counter } />
    <Route path='/fetchdata' component={ FetchData } />
  </Layout>
</App>;

这是我修改后的App组件,适用于此版本:

export class App extends React.Component<AppProps, {}> {
    constructor(props: AppProps) {
        super(props);
        this.state = { stateDescription: 'My Cool App State' };
    }

    public render() {
        return (
            <div className='appContainer'>
                { this.props.children }
            </div>
        );
    }
}