不推荐使用PropTypes以及如何解决它

时间:2017-10-19 05:11:49

标签: node.js react-native

我收到错误

  

未定义不是对象(评估' _react2.PropTypes.string')

在谷歌上搜索时,我发现React.Proptypes已被弃用,而且它本身就已被弃用。

但是,我该如何解决这个问题?

我尝试了以下内容:

1。)npm install -g prop-types --save

2。)部分来自代码

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Text, View, TouchableOpacity } from 'react-native';
import { Actions } from 'react-native-router-flux';
import { Field, reduxForm } from 'redux-form';
import { Container, Input, Button, Item, Spinner } from '../common';
import styles from './authStyle';

const propTypes = {
  handleSubmit: PropTypes.func.isRequired,
  clearState: PropTypes.func.isRequired,
  signUpUser: PropTypes.func.isRequired,
  authError: PropTypes.string.isRequired,
  loading: PropTypes.bool.isRequired,
};

class Signup extends Component {
  constructor(props) {
    super(props);

    this.handleFormSubmit = this.handleFormSubmit.bind(this);
  }
...

但是,我最终遇到了同样的错误,我该如何解决?

更新

import React from 'react';
import PropTypes from 'prop-types';
import { Text, TouchableOpacity } from 'react-native';

const propTypes = {
  children: PropTypes.node.isRequired,
  onPress: PropTypes.func.isRequired,
  buttonStyle: PropTypes.object,
  textStyle: PropTypes.object,
};

const defaultProps = {
  buttonStyle: {},
  textStyle: {},
};

function Button({ onPress, children, buttonStyle, textStyle }) {
  const { button, text } = styles;

  return (
    <TouchableOpacity
      onPress={onPress}
      style={[button, buttonStyle]}
    >
      <Text style={[text, textStyle]}>
        {children}
      </Text>
    </TouchableOpacity>
  );
}

const styles = {
  button: {
    flex: 1,
    alignSelf: 'stretch',
    backgroundColor: '#039be5',
    borderRadius: 3,
    marginTop: 10,
  },
  text: {
    alignSelf: 'center',
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
    paddingTop: 10,
    paddingBottom: 10,
  },
};

Button.defaultProps = defaultProps;
Button.propTypes = propTypes;

export { Button };

1 个答案:

答案 0 :(得分:-1)

改为使用

import PropTypes from 'prop-types';

Button.propTypes = {
    type: PropTypes.string
};