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

Setting state with the selectedRows has unexpected results #2747

Closed
suederade opened this issue Dec 31, 2015 · 10 comments
Closed

Setting state with the selectedRows has unexpected results #2747

suederade opened this issue Dec 31, 2015 · 10 comments
Labels
component: table This is the name of the generic UI component, not the React module!

Comments

@suederade
Copy link

<Table
    fixedHeader={true}
    selectable={true}
    multiSelectable={true}
    height={'100%'}
    onRowSelection={selectedRows => onRowSelect(selectedRows)}>

<TestTable data={this.state.data}
    onHeaderClick={this.sortColumn}
    onRowSelect={this.updateSelectedRows} />

updateSelectedRows(selectedRows) {
    this.setState({selectedRows});
},

When I use this pattern, then the table no longer behaves correctly. Checkboxes don't check and selectedRow array only has one index. I want it passed up because it needs to be passed to another component. But just the presence of the setState causes this.

Also, onRowSelection gets fired when the table is rerendered and there are rows are selected (but not when there are none selected).

@suederade
Copy link
Author

Now with the same code I had in earlier (that worked earlier), it doesn't work now.

updateSelectedRows(selectedRows) {
    console.log('add row to state');
    console.log(selectedRows);
    this.state.data.forEach(obj => {
        obj.checked = false;
    });
    selectedRows.forEach(index => {
        this.state.data[index].checked = true;
    });
    this.setState({data: this.state.data}, () => {console.log(this.state)});
},

EDIT

And now works after undoing and redoing in text editor. Though the first point still doesn't work.

@suederade
Copy link
Author

Nor does this table function correctly with this:

    var selectedTests = [];
    if(selectedRows === 'all') {
        selectedTests = this.state.data;
    } else if(selectedRows !== 'none') {
        selectedRows.forEach(index => {
            selectedTests.push(this.state.data[index]);
        });
    }
    this.setState({selectedTests}, () => {console.log(this.state)});

@suederade
Copy link
Author

Not being able to update the state with the selected rows makes its functionality almost useless.

@oliviertassinari oliviertassinari added the new feature New feature or request label Jan 6, 2016
@suederade
Copy link
Author

Not even being able to select a row (not even animations or the checkbox being clicked) if your onRowSelection function is this.setState({selectedRows}) seems more like a bug than a feature request, in my opinion.

@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Jan 8, 2016
@suederade
Copy link
Author

It seems that setting the state in that function is somehow resetting the state. If I put a timeout, the row is checked then once it fires, it unchecks itself and the TableBody state is reset.

@suederade
Copy link
Author

This is me not knowing enough about React. Need to make sure render isn't called after setting its state.

@oliviertassinari oliviertassinari removed bug 🐛 Something doesn't work new feature New feature or request labels Jan 8, 2016
@oliviertassinari
Copy link
Member

@stevenmwade I'm glad you found a solution. Sometime just exposing the issue help 😁.

@suederade
Copy link
Author

@oliviertassinari Although componentWillReceiveProps and componentClickAway seem to be trying to do some of the same things like setting the selectedRows to an empty array which both run on click away (which I will probably change because I need that to persist).

@issmirnov
Copy link

I'm having the same issue - can you elaborate on your final solution? Trying to setState with the indices of selected rows for later processing. Thanks!

@spoeck
Copy link

spoeck commented Jun 10, 2017

Found a solution:
#1897

 constructor() {
        super();
        this.state = {
            selectedRows: []
        };

        this._onRowSelection = this._onRowSelection.bind(this);
    }


    render() {
        return (
                <Table multiSelectable onRowSelection={this._onRowSelection}>
                    <TableHeader>
                        <TableRow>
                            <TableHeaderColumn>c1</TableHeaderColumn>
                            <TableHeaderColumn>c2</TableHeaderColumn>
                            <TableHeaderColumn>c3</TableHeaderColumn>
                        </TableRow>
                    </TableHeader>
                    <TableBody showRowHover ref={(tableBody) => { this.tableBody = tableBody; }}>
                        {this._renderTableRow()}
                    </TableBody>
                </Table>
        );
    }

    _onRowSelection(rows: Array<number>) {
        this.setState({selectedRows: rows}, () => this.tableBody.setState({ selectedRows: rows }));
    }

@zannager zannager added the component: table This is the name of the generic UI component, not the React module! label Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: table This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

5 participants