类型为“number”的道具“style”无效
Invalid prop `style` of type `number`
import React, { Component } from 'react';
import { Animated } from 'react-native';
import PropTypes from 'prop-types';
class Fade extends React.Component {
Fade.propTypes = {
visible:PropTypes.bool,
style:PropTypes.object,
children:PropTypes.any
};
render() {
const { visible, style, children, ...rest } = this.props;
const combinedStyle = [containerStyle, style];
return (
<Animated.View style={this.state.visible ? combinedStyle : containerStyle} {...rest}>
{this.state.visible ? children : null}
</Animated.View>
);
}
}
我收到以下错误:
提供给 Fade
的 number
类型的无效道具 style
,应为 object
提供给 Fade
的 number
类型的无效道具 style
,应为 object
将 style: PropTypes.object
更改为 ViewPropTypes.style
,如下所示
import React, { Component } from 'react';
import {
Animated,
ViewPropTypes # <= declare
} from 'react-native';
import PropTypes from 'prop-types';
class Fade extends React.Component {
Fade.propTypes = {
visible: PropTypes.bool,
style: ViewPropTypes.style, # <= here changed
children: PropTypes.any
};
...
}
为什么要用ViewPropTypes.style
?
因为这是视图组件的样式属性。
- 视图的样式道具 - ViewPropTypes.style
import React, { Component } from 'react';
import { Animated } from 'react-native';
import PropTypes from 'prop-types';
class Fade extends React.Component {
Fade.propTypes = {
visible:PropTypes.bool,
style:PropTypes.object,
children:PropTypes.any
};
render() {
const { visible, style, children, ...rest } = this.props;
const combinedStyle = [containerStyle, style];
return (
<Animated.View style={this.state.visible ? combinedStyle : containerStyle} {...rest}>
{this.state.visible ? children : null}
</Animated.View>
);
}
}
我收到以下错误:
提供给 Fade
的 number
类型的无效道具 style
,应为 object
提供给 Fade
的 number
类型的无效道具 style
,应为 object
将 style: PropTypes.object
更改为 ViewPropTypes.style
,如下所示
import React, { Component } from 'react';
import {
Animated,
ViewPropTypes # <= declare
} from 'react-native';
import PropTypes from 'prop-types';
class Fade extends React.Component {
Fade.propTypes = {
visible: PropTypes.bool,
style: ViewPropTypes.style, # <= here changed
children: PropTypes.any
};
...
}
为什么要用ViewPropTypes.style
?
因为这是视图组件的样式属性。
- 视图的样式道具 - ViewPropTypes.style