Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // With callbacs:
- class List extends Component {
- constructor(props) {
- super(props);
- props.items.map(item =>
- this.refsCallbacks[item.id] = (el) => this.refs.items[item.id] = el;
- );
- // also you can store refCallbacks in state.
- }
- componentWillReceiveProps(nextProps) {
- if (nextProps.items !== this.props.items) {
- nextProps.items.map(item => {
- if (this.refsCallbacks[item.id]) {
- return;
- }
- this.refsCallbacks[item.id] = (el) => this.refs.items[item.id] = el;
- });
- }
- }
- componentDidMount() {
- this.calculate();
- }
- componentDidUpdate() {
- this.calculate();
- }
- refsCallbacks = {};
- refs = {items: {}};
- calculate() {
- // Calculate width of elements and hide elements that overflow container
- this.refs.items.map(...); // Good
- }
- render() {
- const { items } = this.props;
- return (
- <div className='container'>
- {items.map(item =>
- <div
- ref={this.refCallbacks[item.id]}
- key={item.id}
- className='item'
- >
- {item.content}
- </div>
- )}
- </div>
- );
- }
- }
Add Comment
Please, Sign In to add comment