React-Router not correctly rendering Styled-Components Styles

remoo

The issue is that when I navigate to a 'Post', expanded screen, then use my browser to go back, it is not showing the correct CSS styles, it is using the styles from the expanded view, rather than the 'non-expanded' view. If I toggle a style on the Component using Inspect Element it fixes the issue immediately. I know I could probably circumvent this by adding a back button on the page, and I probably will, but what about the people who use the browser back button?

I am trying to use the same StyledComponent for different pages. Here is the code for my router

<Router>
      <Route
        exact
        path="/"
        render={() => posts.map((post, key) => <Post data={post} key={key} />)}
      />
      <Route
        exact
        path={`/posts/:post`}
        render={currentPath => renderCurrentPost(currentPath)}
      />
    </Router>

All that the second route does is find the correct post to render, then render it.

Here is my code for the specific styled component.

import Styled from 'styled-components';
const Title = Styled.h1`
    @import url('https://fonts.googleapis.com/css?family=Roboto&display=swap');
    font-family: 'Roboto', sans-serif;
    font-style: medium;
    color: ${props => (props.expanded ? `white` : `#111111`)}
    font-size: ${props => (props.expanded ? `5vh` : `1.2em`)}
    margin: 0 0 0 0;
    align-self: flex-end;
    /* display: inline; */
    position: ${props => (props.expanded ? `relative` : ``)};
    top: ${props => (props.expanded ? `70%` : ``)};    
    left: ${props => (props.expanded ? `5%` : ``)};
     `;

export default Title;

I am using props heavily in these components as it will change the way it renders.

Here is the desired style that renders correctly when you first navigate to the landing page.Desired Style

Here is the non-desired styling that happens when you navigate back from a expanded post page using the browser back button.Non-Desire Style

remoo

I answered this myself by slowly removing each CSS line from my styled component to find the problem, which turned out to be this line of code

padding: ${props => (props.expanded ? `` : `0 2% 0 10%`)};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React styled-components not applying styles to components

React/Router not rendering Components

React Router not rendering components

How to do overlapping styles with Styled Components React?

Overriding react components styles with styled component

React router rendering multiple components

Styled components not applying styles

conditional rendering in styled components

React app with styled components and twin.macro set global styles

Share the same styles between two types of component with React Styled Components

Overwriting nested styles in React Bootstrap using Styled-Components

React: Why not just use styles object instead of styled-components?

React Router v4 not rendering components

React router not rendering components due to its structure

Where to put styled components styles

Use 'active' state from React Router in Styled Components

How to inject global styles while rendering server-side with styled-components

Styled Components rendering Styled tag based on props

Conditionally rendering a component with styled components

Can not apply styles to custom components in React Native using styled-components?

Angular Material: Components not styled correctly

Setup for React styled components

React Router or Semantic UI React causing issue with rendering components

Change inline styles to styled-components

idiomatic way to share styles in styled-components?

How to create shared styles using styled components?

Extending styles with styled-components not working

React styled-components - How to render styles based on prop condition or pseudo class?

ReactJS react-router-dom ^5.3.0 not rendering components