How*_*ard 48 eslint react-native
启用了forbid-prop-types规则后,eslint禁止使用style: React.PropTypes.object,shape建议使用.
但是真的有必要为此目的定义所有可用的属性吗?
DEMO.propTypes = {
style: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number,
...
})
};
Run Code Online (Sandbox Code Playgroud)
定义代码太多了!
NiF*_*iFi 49
React Native现在包含ViewPropTypes,将替换View.propTypes.style.用法示例:
import { ViewPropTypes } from 'react-native';
MyComponent.propTypes = {
style: ViewPropTypes.style
};
Run Code Online (Sandbox Code Playgroud)
tia*_*gob 33
View.propTypes.style
Run Code Online (Sandbox Code Playgroud)
要么
Text.propTypes.style
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
DEMO.propTypes = {
style: Text.propTypes.style,
...
};
Run Code Online (Sandbox Code Playgroud)
https://facebook.github.io/react-native/releases/0.21/docs/style.html#pass-styles-around
Dav*_*don 20
一种可能性是使用react-style-proptype包,如下所示:
import stylePropType from 'react-style-proptype';
MyComponent.propTypes = {
style: stylePropType,
};
Run Code Online (Sandbox Code Playgroud)
使用下面的简单代码。
PropTypes.oneOfType([PropTypes.object, PropTypes.array])
Run Code Online (Sandbox Code Playgroud)
因此,您可以将此代码应用于此。
DEMO.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
};
Run Code Online (Sandbox Code Playgroud)
在 RN > 0.57 中测试。
| 归档时间: |
|
| 查看次数: |
23451 次 |
| 最近记录: |