Passing style to child component without using global styles
See original GitHub issueI’ve got a child component, let’s call it Link
.
This is the code for that:
import React from 'react';
import Link from 'next/link';
export default class CustomLink extends React.Component {
render() {
if (this.props.onClick) {
return (
<span className={this.props.className} onClick={this.props.onClick}>
{this.props.children}
</span>
);
}
return (
<Link
href={this.props.href}
params={this.props.params}
as={this.props.as}>
<a className={this.props.className}>{this.props.children}</a>
</Link>
);
}
}
Now say that I import that component within another component, called Header
The code for that is:
import Link from './Link';
export default class Header extends React.Component {
render() {
return (
<div className="root">
<style jsx>{`
.link {
background-color: #fff;
border-bottom: 1px solid #eee;
color: #222;
}
`}
</style>
<Link className="link">Click me</Link>
);
}
}
However, unless I use <style jsx global>
, then unfortunately the style doesn’t get passed to the child component.
I think a related issue is #197, but I haven’t seen any outcome or update on that issue since June.
Is there a solution to fixing this issue?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:17
- Comments:12 (4 by maintainers)
Top Results From Across the Web
How to style child components from parent component's CSS ...
use @Component to add css class to host element and set encapsulation to none. Then reference that class which was added to the...
Read more >How to Style Child Component From Parent In Angular - Medium
Instead of expose all the styles of that component to have a global effect, a slightly better solution is to use the ::ng-deep...
Read more >Styling child component from parent in Angular
The simplest solution for this problem is to set the encapsulation property to ViewEncapsulation. None in the component.
Read more >"proper" way to style child elements via CSS? : r/angular - Reddit
You've got several options: Add the styles to the global stylesheet and make them unique enough to not conflict. Add another stylesheet and ......
Read more >Component styles - Angular
While Angular's emulated style encapsulation prevents styles from escaping a component, it does not prevent global CSS from affecting the entire page.
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
That’s the main point, we’d have to figure out a way to control styles from the parent because passing a prop (eg.
className
) is an half baked solution that won’t work all the times.As for now the best way to do this is to use
.someSelectorInTheParentComponent > :global()
from within the parent component eg.I’ve found a better workaround for this issue.