Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

React warning using styled function #15614

Closed
2 tasks done
yordis opened this issue May 7, 2019 · 11 comments
Closed
2 tasks done

React warning using styled function #15614

yordis opened this issue May 7, 2019 · 11 comments
Labels
duplicate This issue or pull request already exists

Comments

@yordis
Copy link
Contributor

yordis commented May 7, 2019

  • 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 馃

Using the following code snippet

export const styledBy = (property, mapping) => props =>
  mapping[props[property]];

const AppBarContent = styled('div')({
  flexGrow: 1,
  display: 'flex',
  justifyContent: styledBy('alignContent', {
    start: 'flex-start',
    end: 'flex-end',
    center: 'center',
  }),
});


// ....

<AppBarContent alignContent={props.alignContent}>
  {props.children}
</AppBarContent>

Current Behavior 馃槸

index.js:1437 Warning: React does not recognize the alignContent prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase aligncontent instead. If you accidentally passed it from a parent component, remove it from the DOM element.

Steps to Reproduce 馃暪

Link: https://github.com/straw-hat-team/react-admin

  1. yarn start
  2. Notice the console error

Context 馃敠

Your Environment 馃寧

Tech Version
Material-UI 4.0.0-beta.1
React 16.8.6
Browser
TypeScript 3.4.5
etc.
@oliviertassinari
Copy link
Member

The relevant discussion: styled-components/styled-components#439.

@yordis
Copy link
Contributor Author

yordis commented May 7, 2019

@oliviertassinari I tried to follow the mega-thread,

What do we need to do to fix this? I am kind of lost with so much info.

@oliviertassinari
Copy link
Member

@yordis To sum-up. The props are forwarded to the underlying wrapped element. You have to manually stop them.

@oliviertassinari oliviertassinari added discussion package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. labels May 7, 2019
@ggascoigne
Copy link

I think that I'm missing something; is there any way to stop forwarding props to an html element (such as div), while still using styled(). I can see how to do it with makeStyles/useStyles since you get explicit control of what gets passed to your div, but not when using styled.

It is as simple as, don't pass props to styled html elements?

@puema
Copy link

puema commented Jul 9, 2020

Any updates here? It is super annoying to have passed custom props to the underlying dom element. For more complex styling the styled API is then pretty unusable.

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 10, 2020

I had a new look at the issue. emotion and styled-components report a warning, shouldForwardProp is supported by both. You can use the API to ignore the prop.

For instance with styled-components:

import React from 'react';
import { experimentalStyled as styled } from '@material-ui/core/styles';

export const styledBy = (property, mapping) => props =>
  mapping[props[property]];

const AppBarContent = styled('div', {
  shouldForwardProp: prop => prop !== 'alignContent',
})({
  flexGrow: 1,
  display: 'flex',
  justifyContent: styledBy('alignContent', {
    start: 'flex-start',
    end: 'flex-end',
    center: 'center',
  }),
});

export default function Demo() {
  return <div><AppBarContent alignContent="end">hello</AppBarContent></div>
}

However, emotion doesn't support JavaScript prop functions as styled-components does. So the styledBy API isn't usable with emotion. cc @mnajdova for context

@meastes
Copy link

meastes commented Oct 21, 2020

I wanted to give this a try, but unfortunately experiementalStyled doesn't seem to be available in @material-ui/core/styles - at least not in the TypeScript definitions. Is this something that should be available in the stable release version of MUI?

@oliviertassinari
Copy link
Member

@meastes Make sure you use v5.0.0-alpha.13 or upward: https://codesandbox.io/s/unstyledslider-material-demo-forked-d98tg?file=/demo.tsx. It works.

@oliviertassinari oliviertassinari added duplicate This issue or pull request already exists and removed discussion package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. labels Nov 12, 2020
@oliviertassinari
Copy link
Member

Duplicate of #21318

@oliviertassinari oliviertassinari marked this as a duplicate of #21318 Nov 12, 2020
@Puspendert
Copy link

import styled from "styled-components" doesn't have this problem, but import {styled} from "@mui/material" still giving this warning.
Any official solution?

@mnajdova
Copy link
Member

mnajdova commented Dec 2, 2021

See #27512 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

7 participants