mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Simplifications in ReactSpa and ReactReduxSpa
This commit is contained in:
@@ -2,7 +2,7 @@ import * as React from 'react';
|
|||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
import { renderToString } from 'react-dom/server';
|
import { renderToString } from 'react-dom/server';
|
||||||
import { StaticRouter } from 'react-router-dom';
|
import { StaticRouter } from 'react-router-dom';
|
||||||
import { replace } from "react-router-redux";
|
import { replace } from 'react-router-redux';
|
||||||
import { createMemoryHistory } from 'history';
|
import { createMemoryHistory } from 'history';
|
||||||
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
|
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
|
||||||
import routes from './routes';
|
import routes from './routes';
|
||||||
@@ -10,31 +10,28 @@ import configureStore from './configureStore';
|
|||||||
|
|
||||||
export default createServerRenderer(params => {
|
export default createServerRenderer(params => {
|
||||||
return new Promise<RenderResult>((resolve, reject) => {
|
return new Promise<RenderResult>((resolve, reject) => {
|
||||||
// Create memory history to use in the Redux store
|
// Prepare Redux store with in-memory history, and dispatch a navigation event
|
||||||
const history = createMemoryHistory();
|
// corresponding to the incoming URL
|
||||||
const store = configureStore(history);
|
const store = configureStore(createMemoryHistory());
|
||||||
|
|
||||||
// Dispatch the current location so that the router knows where to go
|
|
||||||
store.dispatch(replace(params.location));
|
store.dispatch(replace(params.location));
|
||||||
|
|
||||||
const context : any = {};
|
// Prepare an instance of the application and perform an inital render that will
|
||||||
|
// cause any async tasks (e.g., data access) to begin
|
||||||
|
const routerContext: any = {};
|
||||||
const app = (
|
const app = (
|
||||||
<Provider store={ store }>
|
<Provider store={ store }>
|
||||||
<StaticRouter context={ context } location={ params.location.path } children={ routes } />
|
<StaticRouter context={ routerContext } location={ params.location.path } children={ routes } />
|
||||||
</Provider>
|
</Provider>
|
||||||
);
|
);
|
||||||
|
|
||||||
// Perform an initial render that will cause any async tasks (e.g., data access) to begin
|
|
||||||
renderToString(app);
|
renderToString(app);
|
||||||
|
|
||||||
// If there's a redirection, just send this information back to the host application (Maybe improve this?)
|
// If there's a redirection, just send this information back to the host application
|
||||||
if (context.url) {
|
if (routerContext.url) {
|
||||||
resolve({ redirectUrl: context.url });
|
resolve({ redirectUrl: routerContext.url });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Once the tasks are done, we can perform the final render
|
// Once any async tasks are done, we can perform the final render
|
||||||
// We also send the redux store state, so the client can continue execution where the server left off
|
// We also send the redux store state, so the client can continue execution where the server left off
|
||||||
params.domainTasks.then(() => {
|
params.domainTasks.then(() => {
|
||||||
resolve({
|
resolve({
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import { ApplicationState } from '../store';
|
|||||||
import * as CounterStore from '../store/Counter';
|
import * as CounterStore from '../store/Counter';
|
||||||
import * as WeatherForecasts from '../store/WeatherForecasts';
|
import * as WeatherForecasts from '../store/WeatherForecasts';
|
||||||
|
|
||||||
type CounterProps = CounterStore.CounterState & typeof CounterStore.actionCreators;
|
type CounterProps =
|
||||||
|
CounterStore.CounterState
|
||||||
|
& typeof CounterStore.actionCreators
|
||||||
|
& RouteComponentProps<{}>;
|
||||||
|
|
||||||
class Counter extends React.Component<CounterProps, {}> {
|
class Counter extends React.Component<CounterProps, {}> {
|
||||||
public render() {
|
public render() {
|
||||||
@@ -22,7 +25,7 @@ class Counter extends React.Component<CounterProps, {}> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wire up the React component to the Redux store
|
// Wire up the React component to the Redux store
|
||||||
export default connect<CounterStore.CounterState, {}, RouteComponentProps<{}>>(
|
export default connect(
|
||||||
(state: ApplicationState) => state.counter, // Selects which state properties are merged into the component's props
|
(state: ApplicationState) => state.counter, // Selects which state properties are merged into the component's props
|
||||||
CounterStore.actionCreators // Selects which action creators are merged into the component's props
|
CounterStore.actionCreators // Selects which action creators are merged into the component's props
|
||||||
)(Counter);
|
)(Counter) as typeof Counter;
|
||||||
@@ -67,7 +67,7 @@ class FetchData extends React.Component<WeatherForecastProps, {}> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default connect<WeatherForecastsState.WeatherForecastsState, {}, WeatherForecastProps>(
|
export default connect(
|
||||||
(state: ApplicationState) => state.weatherForecasts, // Selects which state properties are merged into the component's props
|
(state: ApplicationState) => state.weatherForecasts, // Selects which state properties are merged into the component's props
|
||||||
WeatherForecastsState.actionCreators // Selects which action creators are merged into the component's props
|
WeatherForecastsState.actionCreators // Selects which action creators are merged into the component's props
|
||||||
)(FetchData);
|
)(FetchData) as typeof FetchData;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { RouteComponentProps } from 'react-router-dom';
|
||||||
|
|
||||||
export default class Home extends React.Component<{}, {}> {
|
export default class Home extends React.Component<RouteComponentProps<{}>, {}> {
|
||||||
public render() {
|
public render() {
|
||||||
return <div>
|
return <div>
|
||||||
<h1>Hello, world!</h1>
|
<h1>Hello, world!</h1>
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ export default function configureStore(history: History, initialState?: Store.Ap
|
|||||||
const windowIfDefined = typeof window === 'undefined' ? null : window as any;
|
const windowIfDefined = typeof window === 'undefined' ? null : window as any;
|
||||||
// If devTools is installed, connect to it
|
// If devTools is installed, connect to it
|
||||||
const devToolsExtension = windowIfDefined && windowIfDefined.devToolsExtension as () => GenericStoreEnhancer;
|
const devToolsExtension = windowIfDefined && windowIfDefined.devToolsExtension as () => GenericStoreEnhancer;
|
||||||
const middlewares = [thunk, routerMiddleware(history)];
|
|
||||||
const createStoreWithMiddleware = compose(
|
const createStoreWithMiddleware = compose(
|
||||||
applyMiddleware(...middlewares),
|
applyMiddleware(thunk, routerMiddleware(history)),
|
||||||
devToolsExtension ? devToolsExtension() : f => f
|
devToolsExtension ? devToolsExtension() : f => f
|
||||||
)(createStore);
|
)(createStore);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
"@types/react-dom": "^0.14.14",
|
"@types/react-dom": "^0.14.14",
|
||||||
"@types/react-redux": "^4.4.29",
|
"@types/react-redux": "^4.4.29",
|
||||||
"@types/react-router-dom": "^4.0.3",
|
"@types/react-router-dom": "^4.0.3",
|
||||||
"@types/react-router-redux": "^5.0.0",
|
"@types/react-router-redux": "5.0.1",
|
||||||
"@types/redux": "3.5.27",
|
"@types/redux": "3.5.27",
|
||||||
"@types/webpack": "^2.2.0",
|
"@types/webpack": "^2.2.0",
|
||||||
"@types/webpack-env": "^1.13.0",
|
"@types/webpack-env": "^1.13.0",
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
"react-dom": "~15.4.0",
|
"react-dom": "~15.4.0",
|
||||||
"react-redux": "^4.4.5",
|
"react-redux": "^4.4.5",
|
||||||
"react-router-dom": "^4.1.0",
|
"react-router-dom": "^4.1.0",
|
||||||
"react-router-redux": "^5.0.0-alpha.5",
|
"react-router-redux": "5.0.0-alpha.6",
|
||||||
"redux": "^3.6.0",
|
"redux": "^3.6.0",
|
||||||
"redux-thunk": "^2.2.0",
|
"redux-thunk": "^2.2.0",
|
||||||
"style-loader": "^0.13.0",
|
"style-loader": "^0.13.0",
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ import './css/site.css';
|
|||||||
import 'bootstrap';
|
import 'bootstrap';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as ReactDOM from 'react-dom';
|
import * as ReactDOM from 'react-dom';
|
||||||
import { BrowserRouter as Router } from 'react-router-dom';
|
import { BrowserRouter } from 'react-router-dom';
|
||||||
import routes from './routes';
|
import routes from './routes';
|
||||||
|
|
||||||
// This code starts up the React app when it runs in a browser. It sets up the routing configuration
|
// 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.
|
// and injects the app into a DOM element.
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<Router children={ routes } />,
|
<BrowserRouter children={ routes } />,
|
||||||
document.getElementById('react-app')
|
document.getElementById('react-app')
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module.exports = (env) => {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
entry: {
|
entry: {
|
||||||
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'event-source-polyfill', 'isomorphic-fetch', 'react', 'react-dom', 'react-router', 'jquery'],
|
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'event-source-polyfill', 'isomorphic-fetch', 'react', 'react-dom', 'react-router-dom', 'jquery'],
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
path: path.join(__dirname, 'wwwroot', 'dist'),
|
path: path.join(__dirname, 'wwwroot', 'dist'),
|
||||||
|
|||||||
Reference in New Issue
Block a user