Skip to content

[Style] Defining a default style for components #1768

Closed
@yelled3

Description

@yelled3

consider the following use case;
my current project uses custom fonts, which means we want that all Text components have fontFamily: 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

var Fonts = require('./../Fonts');

var DefaultText = React.createClass({
  propTypes: {
    style: Text.propTypes.style
  },
  render() {
    return (
      <Text style={[styles.text, this.props.style]}>{this.props.children}</Text>
    );
  }
});

var styles = StyleSheet.create({
  text: {
    fontFamily: Fonts.Normal
  }
});

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:

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text
  } = React;

var defaultTextStyle = StyleSheet.create({
  text: {
    fontFamily: Fonts.Normal
  }
});

AppRegistry.registerComponentStyle(Text, defaultTextStyle)
// or maybe just
Text.defaultProps.style = defaultTextStyle

// before/after the app is registered?
AppRegistry.registerComponent('MyApp', () => MyApp);

Thoughts?

@ide @JohnyDays /cc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions