I have a styled component that is extending a third-party component:
import Resizable from 're-resizable'; ... const ResizableSC = styled(Resizable)``; export const StyledPaneContainer = ResizableSC.extend` flex: 0 0 ${(props) => props.someProp}px; `; const PaneContainer = ({ children, someProp }) => ( <StyledPaneContainer someProp={someProp} > {children} </StyledPaneContainer> ); export default PaneContainer;
This resulted in the following error in the browser console:
Warning: React does not recognize the
someProp
prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercasesomeProp
instead. If you accidentally passed it from a parent component, remove it from the DOM element
So, I changed the prop to have a data-*
attribute name:
import Resizable from 're-resizable'; ... const ResizableSC = styled(Resizable)``; export const StyledPaneContainer = ResizableSC.extend` flex: 0 0 ${(props) => props['data-s']}px; `; const PaneContainer = ({ children, someProp }) => ( <StyledPaneContainer data-s={someProp} > {children} </StyledPaneContainer> ); export default PaneContainer;
This works, but I was wondering if there was a native way to use props in the styled component without them being passed down to the DOM element (?)
Transient propsIf you want to prevent props meant to be consumed by styled components from being passed to the underlying React node or rendered to the DOM element, you can prefix the prop name with a dollar sign ($), turning it into a transient prop.
You can override an existing styled-components component by passing it to the styled() constructor/wrapper. For example, suppose you have the following styled button element: const Button = styled.
If you wish to style your own existing (or any third-party) React component, then you can simply use the styled-components' styled() constructor. This works as long as the React component you're styling attaches the passed className prop (or style prop, if you're using react-native ) to the DOM element.
To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.
With the release 5.1.0 you can use transient props. This way you do not need an extra wrapper i.e. unnecessary code is reduced:
Transient props are a new pattern to pass props that are explicitly consumed only by styled components and are not meant to be passed down to deeper component layers. Here's how you use them:
const Comp = styled.div` color: ${props => props.$fg || 'black'}; `; render(<Comp $fg="red">I'm red!</Comp>);
Note the dollar sign ($) prefix on the prop; this marks it as transient and styled-components knows not to add it to the rendered DOM element or pass it further down the component hierarchy.
styled component:
const ResizableSC = styled(Resizable)``; export const StyledPaneContainer = ResizableSC.extend` flex: 0 0 ${(props) => props.$someProp}px; `;
parent component:
const PaneContainer = ({ children, someProp }) => ( <StyledPaneContainer $someProp={someProp} // '$' signals the transient prop > {children} </StyledPaneContainer> );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With