question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jakeboone02commented, Sep 28, 2022

@jbailleul I just released v4.5.3 🎉

(This issue will remain open until #370 is merged into main.)

2reactions
jakeboone02commented, Sep 22, 2022

Thank you so much for the detailed report! My thought process while reading this was something like, “Well ackshually it uses a string representation of the path, which is a stabl…wait…oh no…the path is just the group path plus the index…which is NOT STABLE! How did my life get to this point?!”

Seriously though, I can’t believe that issue hasn’t been caught until now. I’ll fix it in v5.0 which I’m working on releasing as fast as I can. Your suggestion to use the id is the correct fix. Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Concurrent Modification Exception : adding to an ArrayList
ConcurrentModificationException occurs when you modify the list (by adding or removing elements) while traversing a list with Iterator .
Read more >
Iterator Invalidation in C++ - GeeksforGeeks
Iterator Invalidation Rules: 1. Insertion · 1. Insertion · 2. Erasure · 3. Resizing ...
Read more >
Why do I need Keys in React Lists? | by Adhithi Ravichandran
Reordering a list, or adding and removing items from a list can cause issues with the component state, when indexes are used as...
Read more >
Lists and Keys - React
Lists and Keys. First, let's review how you transform lists in JavaScript. Given the code below, we use the map() function to take...
Read more >
Jira smart values - issues | Cloud automation Cloud
Use smart values in automation rules to access and manipulate issue data ... Returns a list of issues from a JQL search caused...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found