-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Style] Defining a default style for components #1768
Comments
Setting a default font family seems like a reasonable use case. I don't know about setting other default styles though - those could easily interfere in unexpected ways with any external libraries that you include in your project that build on top of those and force you into a corner where you have to fork and modify those libraries. cc @vjeux |
Any update here? |
Being able to set defaults rather than using a custom text component everywhere is reasonable, and doing it at the Text component level, like |
|
Ah yes, context would be much better. Still not sure it's worth supporting though, since it should be trivial to find-replace a custom text component.
|
Where context would be more powerful - for better or for worse - is when using 3rd party components that contain Text inside of them. Someone who has thought a lot about accessibility, even from the web team, might have ideas about how text should be customized across an entire app. |
@shlomiatar /cc |
@yelled3 - here I'm using context to pass functions down to child components, you could do the same with styles: https://gist.github.com/brentvatne/db1cf9fa16f7fbc514cb |
Here's a basic example with context: class MyText extends React.Component {
contextTypes: {
textStyle: React.PropTypes.object,
},
render: function() {
return (
<Text style={[styles.defaultText, this.context.textStyle]}>{this.props.children}</Text>
);
},
} class MyView extends React.Component {
childContextTypes: {
textStyle: React.PropTypes.object,
},
getChildContext: function() {
return {textStyle: styles.overriddenTextStyle},
},
render: function() {
return (
<MyText>Foo</MyText>
);
},
} |
Hi there! This issue is being closed because it has been inactive for a while. But don't worry, it will live on with ProductPains! Check out it's new home: https://productpains.com/post/react-native/style-defining-a-default-style-for-components |
so what is the best way to override the style some comment/global components like |
@rainchen create a component with your overriden styles, and then use that instead of text, and passthrough the rest of the props. |
I want to advocate my library react-native-tachyons here, which gives you easy |
You can override the |
consider the following use case;
my current project uses custom fonts, which means we want that all
Text
components havefontFamily: Fonts.Normal
. (of course this can be any other style default)my first intuition was to create a new component and use it all across the project, e.g
IMO, this approach is very error prone, since it's very easy to forget about this and use the RN
Text
.I'm interesting to know if currently there's an alternative to this approach?
I think that, just like a web app has a
default.css
, a RN app should provide a way to define defaults - some like:Thoughts?
@ide @JohnyDays /cc
The text was updated successfully, but these errors were encountered: