小编mul*_*dy1的帖子

React - 如何使用属性初始化器语法存根组件方法?

我终于将我的组件更新React.createClass()为ES6类.我在测试中看到的第一个错误是我的一些sinon.stub()调用失败,因为jscodeshift -t react-codemod/transforms/class.js转换了我的组件方法以使用属性初始化程序提议,即

class Foo extends React.Component {
  componentWillMount() {
    this.fetchSomeData(this.props);
  }
  componentWillReceiveProps(nextProps) {
    if (nextProps.someProp !== this.props.someProp) {
      this.fetchSomeData(nextProps);
    }
  }
  fetchSomeData = (props) => {
    ...
  };
  render() {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何fetchSomeData()使用这种新语法存根?我的测试看起来sinon.stub(Foo.prototype, 'fetchSomeData');不再适用,假设因为不再fetchSomeData是原型.

谢谢!

sinon reactjs ecmascript-next

5
推荐指数
1
解决办法
982
查看次数

标签 统计

ecmascript-next ×1

reactjs ×1

sinon ×1