Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.
This repository was archived by the owner on Apr 11, 2018. It is now read-only.

Resizing without too much logic inside componentDidMount #20

Description

@cosminnicula

componentDidMount is an escape hatch that shouldn't be abused; more particular: the resizing logic inside the SliderTrack, SliderTicks and the Slider.

The only reason for doing all the resize work here is because the clientHeight / clientWidth and bottom / left properties are calculated only after a first paint on the screen.

This can be done more cleaner by separating the logic that retrieves these value from the logic that actually calculates the new dimensions. In other words, componentDidUpdate wouldn't contain any resize logic, but rather something like:

this.setState({
  elementClientHeight: element.clientHeight, //or clientWidth
  elementBottom: element.bottom //or left
});

After that you would simply rely on the functions inside SliderModel / SliderUtil, without having to instantiate a new SliderModel with the underlying DOM elements like it’s happening now - e.g. wrapper.getElementsByClassName(styles['slider-track'])[0]. So the SliderModel / SliderUtil would contain only pure functions without any side effects:

render () {
  const trackSize = SliderModel.getTrackSize(elementClientHeight or elementClientWidth, elementBottom or elementLeft); //can return something like {width: ‘123px’} or {height: ‘123px’} or something smarter that merges all these properties and values
  …
  return (
    <div className = {wrapperClasses} style = { Object.assign(this.props.style, trackSize) }>
    …
    </div>
  );
}

This would make the code more declarative and easy to follow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions