2

关于问题的背景故事:

嗨,我有一个反应代码,npm start 可以工作,但 npm test 没有,我的 npm test 可以工作但 npm start 没有的相同代码的版本略有变化。我现在正试图弄清楚如何让我的测试在 npm start 工作的代码上运行。直到完成测试后,我才意识到视觉差异,现在我无法将测试转换为适用于 npm start 工作的代码。代码的不同之处在于我的 npm start 工作代码在 state ={/*multiple things*/}外部,constructor(props){}并且必须function = (value) => {}在 npm test 工作的代码上调用 react 应用程序state ={/*multiple things*/}中的constructor(props){}所有函数,以及反应应用程序中的所有函数不得不被叫去function(value){}工作。

问题:当前错误:

错误

包.json

{
  "private": true,
  "version": "0.0.0",
  "name": "example-react",
  "dependencies": {
    "moment": "^2.22.2",
    "react": "16.4.1",
    "react-dom": "16.4.1",
    "react-scripts": "^1.1.4",
    "react-snapshot": "^1.3.0",
    "react-table": "^6.8.6",
    "recharts": "^1.1.0"
  },
  "devDependencies": {
    "babel-jest": "*",
    "babel-preset-env": "*",
    "babel-preset-react": "*",
    "enzyme": "^3.4.1",
    "enzyme-adapter-react-16": "^1.2.0",
    "jest": "*"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build && react-snapshot",
    "test": "jest --verbose",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000"
}

.babelrc

{
  "presets": ["env", "react"]
}

应用程序.js

import moment from 'moment';
import React from 'react';

class ReactPage extends React.Component {
  constructor(props) {
    super(props)
    this.setTimeMonth = this.setTimeMonth.bind(this)
    this.setTimeDay = this.setTimeDay.bind(this)
  }

  state = {
      CUS_KEY: "",
      Date1: "",
      Date2: "",
      Date3: ""
  }

  setTimeMonth = (time) => {/*code*/}
  setTimeDay = (time) => {/*code*/}

  //some more functions

  render(){
      return();
  }
}
export default ReactPage; 

如何修复此代码,使其不会在编译时崩溃?

4

0 回答 0