Creating an HOC is a good way to handle the topic. Let me share another idea using React Context .
The context allows you to pass data from the parent node to all its children. Each child can choose context access by defining contextTypes in the component definition.
Let's say App.js is your root.
import themingConfig from 'config/themes'; import i18nConfig from 'config/themes'; import ChildComponent from './ChildComponent'; import AnotherChild from './AnotherChild'; class App extends React.Component { getChildContext() { return { theme: themingConfig, i18n: i18nConfig,
Now our ` ChildComponent.js , who wants some theme and lines i18n
class ChildComponent extends React.Component { render() { const { i18n, theme } = this.context; return ( <View style={theme.textBox}> <Text style={theme.baseText}> {i18n.someText} </Text> </View> ); } } ChildComponent.contextTypes = { theme: React.PropTypes.object, i18n: React.PropTypes.object }; export default ChildComponent;
AnotherChild.js , which wants only a theme, but not i18n. It can also be stateless:
const AnotherChild = (props, context) { const { theme } = this.context; return (<Text style={theme.baseText}>{props.myText}</Text>); } AnotherChild.propTypes = { myText: React.PropTypes.string }; AnotherChild.contextTypes = { theme: React.PropTypes.object }; export default AnotherChild;
free soul
source share