Skip to content

Even when setState returns null, it calls cDU lifecycle. #1783

@Ailrun

Description

@Ailrun

Describe the bug
In React, if setState returns null, cDU is not called and so following does not make infinite loop even after cDU is called. (You can check it on this codesandbox by typing text on the input. It prints only one console log at a time.)

export class TestComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  componentDidUpdate() {
    this.setState(() => {
      return null;
    });
  }

  render() {
    return null;
  }
}

However, in enzyme, when I update component, i.e., invoking cDU with above component, will gives me infinite loop like

      at withSetStateAllowed (node_modules/enzyme/build/Utils.js:296:21)
      at ShallowWrapper.<anonymous> (node_modules/enzyme/build/ShallowWrapper.js:511:42)
      at ShallowWrapper.single (node_modules/enzyme/build/ShallowWrapper.js:1735:25)
      at ShallowWrapper.setState (node_modules/enzyme/build/ShallowWrapper.js:510:14)
      at TestComponent.ShallowWrapper.instance.setState (node_modules/enzyme/build/ShallowWrapper.js:190:35)
      at TestComponent.setState [as componentDidUpdate] (TestComponent.js:11:10)
      at node_modules/enzyme/build/ShallowWrapper.js:548:28
      at withSetStateAllowed (node_modules/enzyme/build/Utils.js:300:3)
      at ShallowWrapper.<anonymous> (node_modules/enzyme/build/ShallowWrapper.js:511:42)
      at ShallowWrapper.single (node_modules/enzyme/build/ShallowWrapper.js:1735:25)
      at ShallowWrapper.setState (node_modules/enzyme/build/ShallowWrapper.js:510:14)
      at TestComponent.ShallowWrapper.instance.setState (node_modules/enzyme/build/ShallowWrapper.js:190:35)
      at TestComponent.setState [as componentDidUpdate] (TestComponent.js:11:10)
      at node_modules/enzyme/build/ShallowWrapper.js:548:28
      // and repeat from line 1 to line 7

To Reproduce

  1. git clone https://github.com/Ailrun/enzyme-issue1783
  2. npm i
  3. npm test

Expected behavior
Does not give infinite loop.

Desktop (please complete the following information):

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions