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>
);
}
}
我收到以下错误:
提供给style
的{{1}}类型的道具number
无效,预期为Fade
提供给object
的{{1}}类型的道具style
无效,预期为number
答案 0 :(得分:3)
将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
?
因为这是View Component的风格道具。