2

我正在使用最新的 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 } />
        );
    }
}

任何想法或建议将不胜感激!

4

1 回答 1

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>
        );
    }
}
于 2017-10-31T16:23:48.400 回答