什么是React.js中已弃用的setProps()的替代方案?

时间:2015-07-13 03:12:10

标签: javascript reactjs mocha

我刚刚将项目的React版本升级到13.3,setProps()不再有效。我在这个摩卡测试中使用它,我现在还不确定如何重写它。我有什么选择?

it('reloads search results when props change ', function() {
    var loadResultsSpy = sinon.spy(searchView, 'loadSearchResults');
    var newProps = {searchBy: 'foo', searchTerm: 'bar'};
    searchView.setProps(newProps);
    expect(loadResultsSpy.calledWith(newProps.searchBy, newProps.searchTerm)).toBe(true);
  });

1 个答案:

答案 0 :(得分:6)

在最常见的情况下,React建议只重新渲染顶级组件,如果新渲染具有相同的组件类型,则基本上只更新道具。由于TestUtils.renderIntoDocument只是creating a DOM node and doing a regular render into it的快捷方式,因此类似的内容将起作用(我对您的测试设置做了一些假设):

var searchView, div;

beforeEach(function() {
  var comp = <SearchView searchBy="baz" searchTerm="quix" />;
  div = document.createElement('div');
  searchView = React.render(comp, div);
});

it('reloads search results when props change', function() {
  var loadResultsSpy = sinon.spy(searchView, 'loadSearchResults');
  var comp = <SearchView searchBy="foo" searchTerm="bar" />;
  React.render(comp, div);
  expect(loadResultsSpy.calledWith("foo", "bar")).toBe(true);
});