'number`类型的prop`style`无效

时间:2017-10-24 06:46:02

标签: reactjs react-native eslint

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

1 个答案:

答案 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的风格道具。