How to use the react-router.createMemoryHistory function in react-router

To help you get started, we’ve selected a few react-router examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github department-of-veterans-affairs / vets-website / test / hca / routes.unit.spec.jsx View on Github external
describe('renders correct component', () => {
    const history = createMemoryHistory('/');
    const store = createStore(veteran);
    let tree;

    before(() => {
      // It's perfectly fine in this test to reuse the rendered component. Do that
      // cause it cuts the test time from 1s down to ~0.1s.
      tree = SkinDeep.shallowRender((
        
          
            {routes}
          
        
        ), {
          store // Mock the Redux store context so components render.
        }
      );
github jaredpalmer / razzle / server / server.js View on Github external
server.get('*', (req, res) => {
  const store = configureStore({
    sourceRequest: {
      protocol: req.headers['x-forwarded-proto'] || req.protocol,
      host: req.headers.host
    }
  })
  const routes = createRoutes(store)
  const history = createMemoryHistory(req.originalUrl)
  const { dispatch } = store

  match({ routes, history}, (err, redirectLocation, renderProps) => {
    if (err) {
      console.error(err)
      return res.status(500).send('Internal server error')
    }

    if (!renderProps) {
      return res.status(404).send('Not found')
    }

    const { components } = renderProps

    // Define locals to be provided to all lifecycle hooks:
    const locals = {
github lifechurch / melos / feature-server.js View on Github external
checkAuth(auth).then(nr.createTracer('checkingAuth', (authResult) => {
		const sessionData = Object.assign({}, authResult.userData)
		authResult.userData.password = null
		verifiedAuth = authResult
		const defaultState = getDefaultState(feature)
		let startingState = Object.assign({}, defaultState, { auth: verifiedAuth })
		startingState = mapStateToParams(feature, startingState, params)

		try {
			const history = createMemoryHistory()
			const store = getStore(feature, startingState, history, null)
			loadData(feature, params, startingState, sessionData, store, Locale).then(nr.createTracer('loadData', (action) => {
				const finish = nr.createTracer('finish', () => {
					const RootComponent = getRootComponent(feature)

					if (RootComponent === null) {
						nr.endTransaction()
						res.status(500).send({ error: 4, message: `No root component defined for this feature: ${feature}` })
					}

					getRenderProps(feature, params.url).then(nr.createTracer('getRenderProps', (renderProps) => {
						let html = null
						try {
							html = renderToString()
						} catch (ex) {
								// throw new Error(`Error: 3 - Could Not Render ${feature} view`, ex)
github lynndylanhurley / redux-auth / dummy / src / app.js View on Github external
auth: authStateReducer,
    routing: routerReducer,
    demoButtons,
    demoUi
  });

  // create the redux store
  const store = createStore(
    reducer,
    compose(
      applyMiddleware(thunk)
    )
  );

  let history = (isServer)
    ? createMemoryHistory(currentLocation)
    : browserHistory;

  history = syncHistoryWithStore(history, store);

  // define app routes
  var routes = (
github TobiasWalle / rush / src / app / server / backend-server.tsx View on Github external
match({routes, location: req.originalUrl}, (error: any, nextLocation: Location, nextState: any) => {
        if (error) {
          // Send message if an error occurs
          res.status(500).send(error.message);
        } else if (nextLocation) {
          // Redirect on redirection
          res.redirect(302, nextLocation.pathname + nextLocation.search);
        } else if (nextState) {
          // Create history
          const history = createMemoryHistory();

          // Setup state
          const initialState = {};
          const store = createStore(
            require('../modules/root').reducer,
            initialState,
            getStoreMiddleware(history),
          );

          syncHistoryWithStore(history, store);

          if (DISABLE_SERVER_SIDE_RENDERING) {
            // Just provider Html without SSR
            res.status(200).send(renderToString(
              ,
            ));
github svnm / static-site-generator-webpack-plugin-example / src / index.js View on Github external
export default (locals, callback) => {
  const history = createMemoryHistory()
  const location = history.createLocation(locals.path)

  match({
    routes: Routes,
    location: location
  }, function(error, redirectLocation, renderProps) {
    var html = ReactDOMServer.renderToStaticMarkup(
      <template>
        
      </template>
    );
    callback(null, html)
  })
}
github rocjs / roc-package-web-app-react / packages / roc-package-web-app-react / app / server / setup-for-render.js View on Github external
export default function setupForRender(createStore, url) {
    const basename = ROC_PATH === '/' ? '' : ROC_PATH;

    const completeUrl = basename + url;
    const memoryHistory = createMemoryHistory({
        entries: [completeUrl],
        basename
    });

    const store = createStore ? createStore(memoryHistory) : null;
    const history = store ? syncHistoryWithStore(memoryHistory, store) : memoryHistory;

    return {
        store,
        history,
        url
    };
}
github yusinto / ld-redux / example / src / server / server.js View on Github external
app.use((req, res) =&gt; {
  const history = createMemoryHistory(req.path);
  const store = createStore();

  const matchParams = {
    history,
    routes,
    location: req.originalUrl
  };

  match(matchParams, (error, redirectLocation, renderProps) =&gt; {
    if (error) {
      res.status(500).send(error.message);
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search);
    } else if (renderProps) {
      const reactString = renderToString(
github uhlryk / my-express-react-seed / src / server / clientRoutes / index.js View on Github external
router.use('/*', function(req, res){
  const history = createMemoryHistory(req.originalUrl || '/');
  const initialState = {};
  let staticHTML = ReactDOMServer.renderToString(
    React.createFactory(App)({
      config: clientConfig,
      history,
      initialState
    })
  );

  res.render('index', {
    staticHTML,
    initialState: JSON.stringify(initialState)
  });
});
github lafaulx / colossus / src / app.server.js View on Github external
return new Promise((resolve, reject) =&gt; {
    const memoryHistory = createMemoryHistory(url);
    const store = configureStore(memoryHistory);
    const history = syncHistoryWithStore(memoryHistory, store);

    match({ history, routes, location: url }, (error, redirectLocation, renderProps) =&gt; {
      if (error) {
        reject(error);
      } else if (redirectLocation) {
        reject({
          status: 301,
          url: redirectLocation.pathname + redirectLocation.search,
        });
      } else if (renderProps) {
        performContainerStaticMethod(renderProps, store)
        .then(() =&gt; {
          const content = renderToString(