node.js - PropTypes 已被弃用以及如何解决它

标签 node.js react-native

我收到错误

Undefined is not an object (Evaluating '_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 };

最佳答案

用这个代替

import PropTypes from 'prop-types';

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

关于node.js - PropTypes 已被弃用以及如何解决它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46823183/

相关文章:

javascript - 离开页面时 React Native 不运行函数

javascript - 为什么我不能在浏览器 : You may not have the required environment or OS to run this project 中运行空白的 Cordova 应用程序

Node.js 以同步方式读取行?或者异步循环?

mysql - 如何控制台记录 promise Sequelize 的返回值

javascript - Node.js/Express POST 的 req.body 为空。通过Postman发送POST请求

javascript - 应该在排序函数之后的回调中设置状态吗?

react-native - 如何使用react-native-video播放谷歌驱动器中的视频?

JavaScript setInterval 未正确绑定(bind)到正确的关闭

node.js - "Error: spawn cmd ENOENT"在 Expo 初始化项目中

javascript - 当文本输入聚焦时,如何避免将 View 向上推并覆盖它?