Double-rendering
See original GitHub issuePlease 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:
- Created 5 years ago
- Reactions:2
- Comments:20
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Getting this on 6.6.1.
Edit: found the culprit: I had wrapped everything in
React.StrictMode
which apparently causes double rendering.I found that changing any routes from using
component
prop torender
prop did the trick:Works
Renders 2x