question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please run the following case:

import React from 'react';
import { createMemoryHistory } from 'history';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { Button, Text, View } from 'react-native';
import { Route, Switch, Router } from 'react-router-native';
import { connectRouter, ConnectedRouter } from 'connected-react-router';

const normalHistory = createMemoryHistory();
const reduxHistory = createMemoryHistory();
const rootReducer = combineReducers({
  router: connectRouter(reduxHistory),
});
const store = createStore(rootReducer, undefined);

function TestCase({ name, history }) {
  return (
    <Switch>
      <Route
        path="/"
        exact
        render={() => {
          console.log('render foo', { name });
          return <Button title="foo" onPress={() => history.push('bar')} />;
        }}
      />
      <Route
        path="/bar"
        render={() => {
          console.log('render bar', { name });
          const handlePress = () => {
            setTimeout(() => {
              history.push('/baz');
            }, 1000);
          };
          return <Button title="bar" onPress={handlePress} />;
        }}
      />
      <Route
        path="/baz"
        render={() => {
          console.log('render baz', { name });
          return <Text>baz</Text>;
        }}
      />
    </Switch>
  );
}

export default function Entrypoint() {
  return (
    <React.Fragment>
      <View style={{ padding: 20 }}>
        <Router history={normalHistory}>
          <TestCase name="normal" history={normalHistory} />
        </Router>
      </View>
      <View style={{ padding: 20 }}>
        <Provider store={store}>
          <ConnectedRouter history={reduxHistory}>
            <TestCase name="connected" history={reduxHistory} />
          </ConnectedRouter>
        </Provider>
      </View>
    </React.Fragment>
  );
}

Click on foo -> bar -> baz in both of the routers:

  • “normal” router will output: render baz only once;
  • “connected” router will output render baz twice.

Why?

    "connected-react-router": "5.0.1",
    "react-router-native": "^4.3.0",

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:20

github_iconTop GitHub Comments

20reactions
AlexReffcommented, Dec 19, 2019

Getting this on 6.6.1.

Edit: found the culprit: I had wrapped everything in React.StrictMode which apparently causes double rendering.

16reactions
gregnbcommented, Dec 11, 2018

I found that changing any routes from using component prop to render prop did the trick:

Works

        <Switch>
          <Route path={'login'} render={props => <LoginContainer {...props} />} />
        </Switch>

Renders 2x

        <Switch>
          <Route path={'login'} component={props => <LoginContainer {...props} />} />
        </Switch>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my React component is rendering twice?
Everything seems to be working fine except it renders twice... The first one renders the phone number and zero points. The second time...
Read more >
My React components render twice and drive me crazy
Hmmm, the I render statement got printed just once, so we cannot reproduce double-rendering with a dead simple function component.
Read more >
React useEffect double rendering fix and why you should stop ...
React18 useEffect double renders components in development with Strict mode. Let's see why that's not a problem.
Read more >
React Components rendered twice — any way to fix this?
Let's find out if there is a way to avoid this problem by trying different implementations. A) Functional Component with useState. function App()...
Read more >
AUSTIN LEE: DOUBLE RENDERING | Wallach Art Gallery
Double -Rendering, a solo exhibition of New York-based artist Austin Lee (b. 1983), explores the aesthetic possibilities of combining computer-generated ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found