1

我正在尝试针对旧的反应应用程序运行 jscodeshift。我收到以下错误。

错误

 ERR app/assets/javascripts/components/api_errors/error_body.js.jsx Transformation error (No PropTypes import found!)
Error: No PropTypes import found!
    at addPropTypesImport (/Users/antarr/code/react-codemod/transforms/React-PropTypes-to-prop-types.js:138:11)
    at module.exports (/Users/antarr/code/react-codemod/transforms/React-PropTypes-to-prop-types.js:288:5)

命令

jscodeshift -t ../../react-codemod/transforms/React-PropTypes-to-prop-types.js app/assets/javascripts/ --extensions=js,jsx 

应用程序/资产/javascripts/components/api_errors/error_body.js.jsx

"use strict";
class ErrorBody extends React.Component {
  resolveAllErrors() {
    if (!confirm("Are you sure you want to do this?")) return;

    const { reason } = this.context.router.getCurrentParams();
    ApiErrorActions.resolveErrorForReason(reason);
  }

  render() {
    const { reason } = this.context.router.getCurrentParams();
    let destroyButton;
    if (reason) {
      destroyButton = <button className="btn btn-danger" onClick={this.resolveAllErrors} >Resolve All</button>
    }

    return (
      <div className="error-body col-md-9 col-xs-12 panel panel-default">
        <div className="error-body-header panel-heading">
          <div className="col-xs-2">
            {destroyButton}
          </div>
          <div className="col-xs-10">
            {reason}
          </div>
        </div>
        <ErrorBodyList reason={reason} errors={this.props.errors} />
        <ErrorBodyContent />
      </div>
    )
  }
}

ErrorBody.propTypes = {
  errors: React.PropTypes.array.isRequired
};

ErrorBody.contextTypes = {
  router: React.PropTypes.func,
};

4

1 回答 1

1

导入error_body.js.jsx如下PropTypes

import PropTypes from "prop-types";
...

React.PropTypesReact v15.5. 所以改变propType定义如下:

ErrorBody.propTypes = {
  errors: PropTypes.array.isRequired
};

ErrorBody.contextTypes = {
  router: PropTypes.func,
};
于 2020-07-09T18:43:06.527 回答