State Map

之前我们知道可以使用组合避免重复执行相同的东西这样一种重复模式。我们可以把执行和传递 state 逻辑挪动到被复用很多的低层级的函数中去。

  1. function FancyBoxWithState(
  2. children,
  3. stateMap,
  4. updateState
  5. ) {
  6. return FancyBox(
  7. children.map(child => child.continuation(
  8. stateMap.get(child.key),
  9. updateState
  10. ))
  11. );
  12. }
  13. function UserList(users) {
  14. return users.map(user => {
  15. continuation: FancyNameBox.bind(null, user),
  16. key: user.id
  17. });
  18. }
  19. function FancyUserList(users) {
  20. return FancyBoxWithState.bind(null,
  21. UserList(users)
  22. );
  23. }
  24. const continuation = FancyUserList(data.users);
  25. continuation(likesPerUser, updateUserLikes);