Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array delete is not working (UI became out-of-sync of model) #22

Open
cguedes opened this issue Apr 27, 2016 · 17 comments
Open

Array delete is not working (UI became out-of-sync of model) #22

cguedes opened this issue Apr 27, 2016 · 17 comments

Comments

@cguedes
Copy link

cguedes commented Apr 27, 2016

In the following GIF image you can see that:

  1. I'm adding three elements to the array (A, B and C)
  2. I'm removing the second element (B)
  3. The model is fine (with values A and C)
  4. But in the UI we see that B remains visible (the last element is the one being removed - not rendered)

react-schema-form issue with array delete

This happens because you are using array's index as the key for each li component rendered in the Array react component.

React uses key prop to improve performance. From the documentation you can read the following:

Keys should be stable, predictable, and unique. Unstable keys (like those produced by Math.random()) will cause many nodes to be unnecessarily re-created, which can cause performance degradation and lost state in child components.

I don't know a solution for this. But the solution should be around defining a stable KEY for the array elements internally tracked by react-schema-form.


  • You can try this yourself in the online playground
  • react-schema-form version used: 0.2.8
@stevehu
Copy link
Contributor

stevehu commented Apr 27, 2016

@cguedes Thanks a lot. This is the best issue I have seen on github.

@nicklasb
Copy link
Contributor

@cguedes I just saw this, and agree with @stevehu, if all were like this.
@cguedes, what did you use to make the gif?

@cguedes
Copy link
Author

cguedes commented Apr 28, 2016

@stevehu @nicklasb Tanks for your kind words.

@nicklasb I've used Recordit. Is an awesome tool to take "video screenshots".

@nicklasb
Copy link
Contributor

I just tried it, yeah that is really a killer app. Thanks for the tip!

@stevehu
Copy link
Contributor

stevehu commented May 20, 2016

@cguedes I have tried to use UUID but the updated field keeps refreshing. It is not easy to fix it and I need more time to figure it out.

@devel-pa
Copy link
Contributor

devel-pa commented Oct 20, 2016

I don't know if is already solved as it's here for quite some time, I have an idea, not tested.

How about transforming arrays into objects when setting the scope and invers transformation (how to detect it has been an array?) when exporting the scope.

lodash:
_.invert(_.invert(['a', 'b', 'c']))

PS: not necesarely needed to use invert for making the IDs, but a special ID that can be detected when exporting

@kevadkins
Copy link

I ran into this issue and the key to fixing it (pun intended) is to have a unique key on the list item in the Array component.

I used lodash _.uniqueId()

<li key={_.uniqueId(i)} className="list-group-item">

I'd already defined my own Array component (basically just a copy of it), so was able to do this without changing the original component in the src.

Thanks @cguedes for pointing me in the right direction!

@stevehu
Copy link
Contributor

stevehu commented Oct 21, 2016

@devel-pa @kevadkins I have tried with unique keys and it doesn't work for me. React refreshes and creates brand new object and my browser dead eventually. My implementation was not the same as yours though. Could you please test your solution to verify if it works?

@stevehu stevehu added the bug label Nov 5, 2016
@maplechori
Copy link
Contributor

I resolved this by embedding SchemaForm inside another component, once that component has been created we don't touch that model.

@stevehu
Copy link
Contributor

stevehu commented Jan 22, 2017

That is very interesting. If you don't touch the model. you don't have the issue. what if you remove one of the items in the array? Are you updating the model in schema form or updating the model in your wrapper component?

@stevehu stevehu closed this as completed Jan 22, 2017
@maplechori
Copy link
Contributor

I have a component that maps over a group of types, each type holds a form/schema/model. I render the one that is selected.

@stevehu
Copy link
Contributor

stevehu commented Jan 22, 2017

@maplechori Is your component open sourced? Or could you please share your component so that someone can be inspired to find a generic solution to resolve the problem?

@stevehu stevehu reopened this Jan 22, 2017
@cguedes
Copy link
Author

cguedes commented Jan 22, 2017

@stevehu you can check this in the live demo you provide (sample: simple array)

print screen

image

animated gif

yybfzzpz7j

@maplechori
Copy link
Contributor

@stevehu I'll try to implement a similar component this week.

@stevehu
Copy link
Contributor

stevehu commented Jan 23, 2017

Excellent. Thanks.

@maplechori
Copy link
Contributor

Seems the bug that I saw is not the one mentioned here. No luck with this one yet.

@MrSaints
Copy link
Contributor

MrSaints commented Dec 4, 2017

I encountered this problem last week. I tried to resolve it, but I wasn't able to isolate the problem. The models are definitely correct, but the UI itself is not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants