Skip to content

Instantly share code, notes, and snippets.

@edrpls
Created July 25, 2019 18:59
Show Gist options
  • Save edrpls/0173cfe64568996f8fb2906a764da306 to your computer and use it in GitHub Desktop.
Save edrpls/0173cfe64568996f8fb2906a764da306 to your computer and use it in GitHub Desktop.
// Clients.jsx
import React from 'react';
// import flux actions
import ClientActions from 'ClientActions';
// import flux store
import ClientStore from 'ClientStore';
class Clients extends React.Component {
// Set the initial state
constructor(props) {
super(props);
const { clients } = ClientStore.getState();
this.state = { clients };
}
// Set flux listener
componentDidMount() {
ClientStore.addChangeListener(this._onChange);
// Request clients from server
ClientActions.getClients();
}
// remove flux listener on unmount
componentWillUnmount() {
ClientStore.removeChangeListener(this._onChange);
}
// update the state when flux emits a change event
_onChange = () => {
const { clients } = ClientStore.getState();
this.setState({ clients });
};
_reverseOrder = () => {
// previously, using Flux:
// ClientActions.toggleSorting();
// now with Redux:
this.props.toggleSorting();
};
render() {
return (
<div>
<button type="button" onClick={this._reverseOrder}>
Reverse order
</button>
<ul>{this.state.clients.map(client => <li key={client.id}>{client.name}</li>)}</ul>
</div>
);
}
}
export default Clients;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment