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

Newly rendered DOM elements not sortable #6

Closed
kjanoudi opened this issue Mar 12, 2016 · 3 comments · Fixed by #7
Closed

Newly rendered DOM elements not sortable #6

kjanoudi opened this issue Mar 12, 2016 · 3 comments · Fixed by #7

Comments

@kjanoudi
Copy link
Contributor

When new items are added or a set of items is replaced via new props, those new items/replaced items have not had Sortable.create called on them, and therefore their DOM elements are not sortable.

This is due to having the Sortable.create call only in componentDidMount but not also in componentDidUpdate

@cheton
Copy link
Collaborator

cheton commented Mar 13, 2016

How about calling this.setState within componentWillReceiveProps?

@Sortable(options)
class SortableList extends React.Component {
    state = {
        items: this.props.items
    };
    componentWillReceiveProps(nextProps) {
        this.setState({ items: nextProps.items });
    }
    render() {
        :   :   :
    }
}

I can get it work with this example, and here is a demo.

@kjanoudi
Copy link
Contributor Author

@cheton that works if the render function always returns a list. However, in the event that the list of items is empty, in my use case, i am returning a message that says "This list has no items", and the render function never gets to the portion where a list is rendered. Perhaps this is a design flaw on my end by not extracting the sortable portion into it's own component?

Flipping back and forth between lists that have no items and lists that have some items results in sortable not working

render() {
        const { items } = this.state
        if (!items || !items.length) {
            return <p>There are no items in this category.</p>
        }

        return (
            <div>
                <ul ref="list" >
                    {items.map((i, k) =>
                        <li key={k} >
        ....
       )

@cheton
Copy link
Collaborator

cheton commented Mar 13, 2016

I just realised what you have encountered. I will take a look at your PR and merge it soon. Thanks!

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

Successfully merging a pull request may close this issue.

2 participants