react-native prop type for text style

╄→尐↘猪︶ㄣ 提交于 2020-01-31 03:42:07

问题


i have component with a simple structure and a <Text> somewhere inside the tree for which i want to pass in a style. Which works perfectly but for the proptypes validation.

The basic setup is not much more than that

export default class Component extends PureComponent {
    render() {
        return (<View><Text style={this.props.style}>Some text</Text></view>);
    }
}

Component.defaultProps = {
    style: null,
};

Component.propTypes = {
    style: ViewPropTypes.style,
};

The problem is that the ViewPropTypes.style does not contain i.e. color key. So providing a style with a color is invalid and produces a warning. I tried to import TextStylePropTypes as i found in https://github.com/facebook/react-native/blob/master/Libraries/Text/TextStylePropTypes.js but it is undefined.

Any advice on what to do?


回答1:


For anybody trying to achieve this seems like View.propTypes.style is deprecated while Text.propTypes.style is not.




回答2:


As the passed style prop is for the Text node, use Text.propTypes.style as shown below...

Component.propTypes = {
    style: Text.propTypes.style
};


来源:https://stackoverflow.com/questions/48565754/react-native-prop-type-for-text-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!