Guest User

Untitled

a guest
Feb 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. // With callbacs:
  2. class List extends Component {
  3. constructor(props) {
  4. super(props);
  5. props.items.map(item =>
  6. this.refsCallbacks[item.id] = (el) => this.refs.items[item.id] = el;
  7. );
  8. // also you can store refCallbacks in state.
  9. }
  10. componentWillReceiveProps(nextProps) {
  11. if (nextProps.items !== this.props.items) {
  12. nextProps.items.map(item => {
  13. if (this.refsCallbacks[item.id]) {
  14. return;
  15. }
  16. this.refsCallbacks[item.id] = (el) => this.refs.items[item.id] = el;
  17. });
  18. }
  19. }
  20. componentDidMount() {
  21. this.calculate();
  22. }
  23. componentDidUpdate() {
  24. this.calculate();
  25. }
  26. refsCallbacks = {};
  27. refs = {items: {}};
  28. calculate() {
  29. // Calculate width of elements and hide elements that overflow container
  30. this.refs.items.map(...); // Good
  31. }
  32. render() {
  33. const { items } = this.props;
  34.  
  35. return (
  36. <div className='container'>
  37. {items.map(item =>
  38. <div
  39. ref={this.refCallbacks[item.id]}
  40. key={item.id}
  41. className='item'
  42. >
  43. {item.content}
  44. </div>
  45. )}
  46. </div>
  47. );
  48. }
  49. }
Add Comment
Please, Sign In to add comment