react-sortable-hoc icon indicating copy to clipboard operation
react-sortable-hoc copied to clipboard

Dealing with dynamically added items (fiddle inside)

Open codetipi opened this issue 10 years ago • 1 comments

Hey Clauderic,

Just played around with this, and it's a great little component! Great job :)

One stumble-block I ran into though was when dealing with dynamically added items. Take a peek at this fiddle:

https://jsfiddle.net/t9va06g5/1/

If you click the add more button, then drag an existing item of the list, it's only then that the newly added item is rendered. Do you know if there is any way to re-render the list when new items are added like this?

Thanks again.

codetipi avatar Oct 04 '16 13:10 codetipi

Hey @codetipi, thanks for the kind words :)

This isn't specifically a problem with react-sortable-hoc, it has to do with how React handles state updates.

You were calling this.state.items.push(...) directly. When you do this, React has no way to know that the state has changed. You need to explicitly call this.setState({...}) if you want to see your component update immediately.

Here's an updated fiddle https://jsfiddle.net/t9va06g5/3/

clauderic avatar Oct 04 '16 13:10 clauderic