Guest User

Untitled

a guest
Jan 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. // Without Redux
  2. class NoRedux extends Component {
  3. state = {
  4. dogs: null,
  5. cats: null,
  6. resourcesLoaded: false,
  7. };
  8.  
  9. componentWillMount() {
  10. Promise.all([api.loadDogs(), api.loadCats()])
  11. .then((dogs, cats) => {
  12. this.setState({
  13. dogs,
  14. cats,
  15. resourcesLoaded: true
  16. });
  17. });
  18. }
  19.  
  20. render() {
  21. const { dogs, cats, resourcesLoaded } = this.state;
  22.  
  23. return (
  24. {!resourcesLoaded && (
  25. <MyFancyLoader />
  26. )}
  27. {resourcesLoaded && (
  28. <AnimalStuff dogs={dogs} cats={cats} />
  29. )}
  30. );
  31. }
  32. }
  33.  
  34. module.exports = NoRedux;
  35.  
  36. // With Redux
  37. class YesRedux extends Component {
  38. static propTypes = {
  39. loadDogs: PropTypes.func.isRequired,
  40. loadCats: PropTypes.func.isRequired,
  41. dogs: PropTypes.arrayOf(PropTypes.shape(DOG_SHAPE)),
  42. cats: PropTypes.arrayOf(PropTypes.shape(CAT_SHAPE)),
  43. };
  44.  
  45. componentWillMount() {
  46. loadDogs();
  47. loadCats();
  48. }
  49.  
  50. render() {
  51. const { dogs, cats } = this.props;
  52. const resourcesLoaded = dogs !== null && cats !== null // what is correct way to indicate "not loaded" in redux?
  53.  
  54. return (
  55. {!resourcesLoaded && (
  56. <MyFancyLoader />
  57. )}
  58. {resourcesLoaded && (
  59. <AnimalStuff dogs={dogs} cats={cats} />
  60. )}
  61. );
  62. }
  63. }
  64.  
  65. module.exports = connect(
  66. (state) => ({
  67. dogs: reducer.getDogs(state), // what if this getter requires a prop of the component?
  68. cats: reducer.getCats(state),
  69. }),
  70. {
  71. loadDogs: reducer.loadCats,
  72. loadCats: reducer.loadDogs,
  73. }
  74. )(YesRedux);
Add Comment
Please, Sign In to add comment