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.

Wrong css injection order with multiple react apps

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

Css injection order should be like this: App1 MUI CSS App1 overrides CSS App2 MUI CSS App2 overrides CSS

Current Behavior 😯

Css injection order is currently like this: App1 MUI CSS App1 overrides CSS App2 overrides CSS App2 MUI CSS

This is how it looks on Chrome devtools:

ordering

App on Chrome:

app

Steps to Reproduce 🕹

  1. Create a page with 2 react apps that both use MUI
  2. Create some custom styles on each app
  3. View on the browser, only the first app overrides are visible

Your Environment 🌎

Tech Version
Material-UI v3.9.0
React 16.7.0
Browser Chrome
TypeScript 3.3.0

App1 Code

import { Button, createGenerateClassName, createStyles, jssPreset, withStyles } from '@material-ui/core';
import { create } from 'jss';
import * as React from 'react';
import JssProvider from 'react-jss/lib/JssProvider';

const jss = (create as any)({ plugins: [...jssPreset().plugins] });

const generateClassName = createGenerateClassName({ productionPrefix: 'app1_' });

const styles = createStyles({
  button: {
    backgroundColor: 'blue',
  },
});

export const App = withStyles(styles)(({ classes }) => (
  <JssProvider jss={jss} generateClassName={generateClassName}>
    <Button className={classes.button}>App1</Button>
  </JssProvider>
));

App2 Code

import { Button, createGenerateClassName, createStyles, jssPreset, withStyles } from '@material-ui/core';
import { create } from 'jss';
import * as React from 'react';
import JssProvider from 'react-jss/lib/JssProvider';

const jss = (create as any)({ plugins: [...jssPreset().plugins] });

const generateClassName = createGenerateClassName({ productionPrefix: 'app2_' });

const styles = createStyles({
  button: {
    backgroundColor: 'red',
  },
});

export const App = withStyles(styles)(({ classes }) => (
  <JssProvider jss={jss} generateClassName={generateClassName}>
    <Button className={classes.button}>App1</Button>
  </JssProvider>
));

Example:

https://codesandbox.io/s/wyonzjzyw7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gilamrancommented, Mar 13, 2019

I’ve managed to solve this by providing an insertionPoint for each app.

function createJss(appId: string) {
  const comment = `${appId}-jss-insertion-point`;
  const styleNode = document.createComment(comment);
  document.head.insertBefore(styleNode, document.head.firstChild);

  const jssOptions: JSSOptions = jssPreset();
  const plugins: ReadonlyArray<JSSPlugin> = [...jssOptions.plugins, rtl() as JSSPlugin];
  return create({...jssOptions, plugins, insertionPoint: comment});
}

If this is a good enough solution (Still not sure what’s the problem is), you can close this issue. And thanks for the fast response!

0reactions
oliviertassinaricommented, Mar 13, 2019

I’ve managed to solve this by providing an insertionPoint for each app.

@gilamran You are using two different JSS instances, it’s the correct solution 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong css injection order with multiple react apps #14338
I have searched the issues of this repository and believe that this is not a duplicate. Expected Behavior. Css injection order should be...
Read more >
Bad injection CSS order when change history with React ...
I having an issue with CSS injection order using material-ui components and theme providers, when I use react router dom .
Read more >
Styling in React: 5 ways to style React apps - LogRocket Blog
In this article, we will review styling React components with inline styling, styled-components, CSS modules, Tailwind CSS, and Sass and CSS ...
Read more >
Releases - styled-components
Fix speedy rule insertion when css contains multiple rules by ... Backport fix where classnames are composed in the wrong order if custom...
Read more >
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
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