Skip to content

the key used when iterating on rule list is causing state issues #374

@jbailleul

Description

@jbailleul

Summary

Hello! I am using the library alongside with custom controlElements and in my implementation of valueEditor, I'm facing state issues that seem to be caused by non-optimal use of the key property during iterations.

Description

Let's say I have two rules, rule#1 and rule#2. When I delete the rule#1 thanks to the Remove rule button, it appears that React tries to use the UI instance of the deleted rule#1 to put the rule#2's content into it (e.g. all the props including field, operator, value..). To illustrate that, in the default ValueEditor, I've tested to add console.log of the operator and of the previous render's operator, and here is what's happening when deleting the first rule:

Sept-21-2022 16-22-47

=> React seems to be trying to fill the Last name in rule's content into the instance of the former First name begins with rule.

After doing some digging into the library's source code, it appears in RuleGroup.tsx#line304 that when iterating on rules, you use indexes as the key to repeated elements.

React recommends to use unique identifiers as keys: indeed, if the order can change, using indexes "can negatively impact performance and may cause issues with component state", and that's precisely the problem I'm encountering.

Suggestion

Would that be possible for you to use unique identifiers for this key, such as the rule's id for example?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions