Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

suggestion: provide an alternative which puts the row data into state, instead of properties #124

Closed
killerswan opened this issue Mar 3, 2015 · 2 comments

Comments

@killerswan
Copy link

Today I put together this class, so that I can make objects which I can call setState on to update in place, to add more data to a Reactable table, on demand.

Is there a more graceful way? Is this pattern going to be common enough you want me to stick this in a pull request?

    var UpdateableTable = React.createClass({
        // on creation, specify the `initial_data` property
        // to modify later, update `data` with `setState`

        getDefaultProps: function() {
            var defaultProps = {
                initial_data: null,
            };
            return defaultProps;
        },

        getInitialState: function() {
            // initialize with a dummy row (see #59)
            if (null === this.props.initial_data) {
                console.log('UpdateableTable expects initial_data not to be empty');
            }
            return {
                data: this.props.initial_data,
            };
        },

        render: function() {
            // use data from state, rather than properties
            this.props.data = this.state.data;
            return React.createElement(Reactable.Table, this.props);
        }
    });
@glittershark
Copy link
Owner

I've always felt that the best way to conform to the React architectural standards was to let users do the wrapping you're doing themselves - keep in mind that the use case of calling setState outside of a component is not really the way React recommends doing things - the state should exist in some sort of parent component and get set in methods on that parent component (ie, as a result of AJAX calls, user input, etc.)

@killerswan
Copy link
Author

I see, so then the Reactable.Table is the only thing touching its own state, and the parent is the only thing touching its own, etc.

Thanks!

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

No branches or pull requests

2 participants