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

Doesn't work with mapped SortableItems #26

Open
dijs opened this issue Jan 25, 2016 · 15 comments
Open

Doesn't work with mapped SortableItems #26

dijs opened this issue Jan 25, 2016 · 15 comments

Comments

@dijs
Copy link

dijs commented Jan 25, 2016

This works:

render() {
  return <Sortable>
      <DemoHOCItem className="item-1" sortData="react" key={1}>
        React
      </DemoHOCItem>
     <DemoHOCItem className="item-2" sortData="angular" key={2}>
        Angular
      </DemoHOCItem>
    </Sortable>
}

This doesnt:

function renderItem(n, index) {
return <DemoHOCItem
      className={'item-' + index}
      sortData={n}
      key={index}>{n}</DemoHOCItem>
}

render() {
  return <Sortable>
     {items.map(renderItem)}
 </Sortable>
}

And by doesn't work, I mean nothing is rendered within sortable.

If I render the {items.map(renderItem)} outside of <Sortable>, it works...

Very odd.

@jasonslyvia
Copy link
Owner

I've tweaked the demo to return mapped items and it just works, please check it out.

function renderItem(num, index) {

@dijs
Copy link
Author

dijs commented Jan 26, 2016

Okay the issue lies within using redux. Still a mystery. But I made a project which shows the issue.

example.zip

Install and run with:

npm install
npm start
open http://localhost:3000

Just try clicking the "+1" button a few times which calls a redux action which increments the counter.

The counter is obviously working, but for some reason, Sortable is not rendering the components.

Thanks

@jasonslyvia
Copy link
Owner

If you're adding or removing children of Sortable dynamically, you should add a different key to <Sortable> every time children change.

Check the notice section of docs for more information.

https://github.com/jasonslyvia/react-anything-sortable#notice

@jasonslyvia
Copy link
Owner

Also you can refer to this demo for handling dynamic children modification.

@JonathanWi
Copy link

@jasonslyvia There is a bug on an edge case (tried with your updated demo also): Mapped items + fixed items breaks the dragging capabilities. I'm not sure why, currently investigating, but could use your help on this one!

Just try to add a fixed item in your demo, and you will see what's happening :)

Cheers

@jasonslyvia jasonslyvia reopened this Nov 14, 2016
@jasonslyvia
Copy link
Owner

Sure, I'll look into it.

@JonathanWi
Copy link

Thanks!

In the render function, you can try this:

<div className="dynamic-demo">
        <button onClick={::this.handleAddElement}>Add 1 element</button>
        <Sortable key={this._sortableKey} onSort={::this.handleSort}>
          {this.state.arr.map(renderItem, this)}
          <span>Hello</span>
        </Sortable>
      </div>

Let me know what you think

@jasonslyvia
Copy link
Owner

Have you tried add dynamic props to <Sortable>?

@JonathanWi
Copy link

@jasonslyvia Yes, no luck either

@jasonslyvia
Copy link
Owner

Also removing key?

@JonathanWi
Copy link

@jasonslyvia Same thing :)

@jasonslyvia
Copy link
Owner

Alright then, I will test it myself.

@JonathanWi
Copy link

@jasonslyvia Any news on this? Thanks!

@jasonslyvia
Copy link
Owner

@JonathanWi Sorry been busy these days, this behaviour is confirmed not working as expected, gonna need more time digging why.

@jasonslyvia
Copy link
Owner

Btw, I think it's a totally different issue from the original one, do you care posting a new issue so we can move discussion there?

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

No branches or pull requests

3 participants