React Native - 来自 PropTypes.bool 的 "Cannot read property 'bool' of undefined"

React Native - "Cannot read property 'bool' of undefined" from PropTypes.bool

我正在尝试将节点包用于我从 https://github.com/ptomasroos/react-native-multi-slider 获得的 React Native 多滑块,但它给了我错误 "Cannot read property 'bool' of undefined." 当我追溯到这个错误的位置时来自,它来自文件中的 PropTypes.bool 行....

'use strict';

var React = require('react');
var ReactNative = require('react-native');

var {
  PropTypes
} = React;
var {
  View
} = ReactNative;

var BasicMarker = React.createClass({

  propTypes: {
    pressed: PropTypes.bool,
    pressedMarkerStyle: View.propTypes.style,
    markerStyle: View.propTypes.style
  },

  render: function () {
    return (
      <View
        style={[this.props.markerStyle, this.props.pressed && this.props.pressedMarkerStyle]}
      />
    );
  }
});

var mockProps = {
  values: [0],
  onValuesChangeStart: function () {
    console.log('press started');
  },
  onValuesChange: function (values) {
    console.log('changing', values);
  },
  onValuesChangeFinish: function (values) {
    console.log('changed', values);
  },
  step: 1,
  min:0,
  max:10,
  selectedStyle: {
    backgroundColor: 'blue'
  },
  unselectedStyle: {
    backgroundColor: 'grey'
  },
  containerStyle: {
    height:30,
  },
  trackStyle: {
    height:7,
    borderRadius: 3.5,
  },
  touchDimensions: {
    height: 30,
    width: 30,
    borderRadius: 15,
    slipDisplacement: 30,
  },
  markerStyle: {
    height:30,
    width: 30,
    borderRadius: 15,
    backgroundColor:'#E8E8E8',
    borderWidth: 0.5,
    borderColor: 'grey',
  },
  customMarker: BasicMarker,
  pressedMarkerStyle: {
    backgroundColor:'#D3D3D3',
  },
  sliderLength: 280
};

module.exports = mockProps;

我在某处读到在使用较新版本的 React Native 时需要专门安装道具类型,所以我 运行

npm install --save prop-types

从我项目的根目录添加代码行

var PropTypes = require('prop-types');

在文件顶部替换了之前 PropTypes 的定义,但这并没有解决我的问题。然后它在

行出错
pressedMarkerStyle: View.propTypes.style

说 propTypes 未定义。我应该如何解决这个问题?

您可以先像这样修改导入来解决这个问题:

var React = require('react');
var { ViewPropTypes } = require('react-native');
var PropTypes = require('prop-types');

然后像这样使用它:

propTypes: {
   pressed: PropTypes.bool,
   pressedMarkerStyle: ViewPropTypes.style,
   markerStyle: ViewPropTypes.style
},