Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created March 26, 2015 00:33
Show Gist options
  • Save aaronpowell/c5700b3d4f1612fc6df5 to your computer and use it in GitHub Desktop.
Save aaronpowell/c5700b3d4f1612fc6df5 to your computer and use it in GitHub Desktop.

Revisions

  1. aaronpowell created this gist Mar 26, 2015.
    18 changes: 18 additions & 0 deletions UserList.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    var UserList = React.createClass({
    render() {
    return (
    <table>
    <thead>
    <tr>
    <th>Name</th>
    <th></th>
    </tr>
    </thead>

    <tbody>
    {this.props.users.filter(user => user.enabled).map(user => <UserRow user={user} />)}
    </tbody>
    </table>
    );
    }
    });
    18 changes: 18 additions & 0 deletions UserRow.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    var UserRow = React.createClass({
    contextTypes: {
    userService: React.PropTypes.object
    },

    render() {
    return (
    <tr>
    <td>{this.props.user.name}</td>
    <td><button onClick={this.disable}>Disable</td>
    </tr>
    );
    },

    disable() {
    this.context.userService.disable(this.props.userId);
    }
    });
    30 changes: 30 additions & 0 deletions UserView.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    var UserView = React.createClass({
    childContextTypes: {
    userStore: React.PropTypes.shape({
    disable: React.PropTypes.func
    })
    },

    getInitialState() {
    return {
    users: []
    }
    },

    getChildContext() {
    return {
    userStore: {
    disable: userId =>
    xhr.post(`/api/user/${userId}/disable`)
    .then(() => xhr.get(`/api/user`))
    .then(users => this.setState({ users }));
    }
    };
    },

    render() {
    return (
    <UserList users={this.state.users} />
    );
    }
    });