Skip to content

Commit

Permalink
feat(src): move instance creation in componentWillMount
Browse files Browse the repository at this point in the history
* saves extra render step compared with componentDidMount
* use ExecutionEnvironment to check canUseDOM
* depends on fbjs@^0.3.1
  • Loading branch information
tomchentw committed Oct 13, 2015
1 parent 818d7a0 commit 91d5790
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"tomchentw-npm-dev": "^3.1.0"
},
"dependencies": {
"fbjs": "^0.3.1",
"google-maps-infobox": "^1.1.13",
"invariant": "^2.1.1"
},
Expand Down
9 changes: 8 additions & 1 deletion src/Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as CircleCreator,
circleDefaultPropTypes,
Expand Down Expand Up @@ -45,7 +49,10 @@ export default class Circle extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...circleProps} = this.props;
const circle = CircleCreator._createCircle(mapHolderRef, circleProps);

Expand Down
9 changes: 8 additions & 1 deletion src/DirectionsRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as DirectionsRendererCreator,
directionsRendererDefaultPropTypes,
Expand Down Expand Up @@ -41,7 +45,10 @@ export default class DirectionsRenderer extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...directionsRendererProps} = this.props;
const directionsRenderer = DirectionsRendererCreator._createDirectionsRenderer(mapHolderRef, directionsRendererProps);

Expand Down
9 changes: 8 additions & 1 deletion src/DrawingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as DrawingManagerCreator,
drawingManagerDefaultPropTypes,
Expand Down Expand Up @@ -37,7 +41,10 @@ export default class DrawingManager extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...drawingManagerProps} = this.props;
const drawingManager = DrawingManagerCreator._createDrawingManager(mapHolderRef, drawingManagerProps);

Expand Down
9 changes: 8 additions & 1 deletion src/InfoWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as InfoWindowCreator,
infoWindowDefaultPropTypes,
Expand Down Expand Up @@ -37,7 +41,10 @@ export default class InfoWindow extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, anchorHolderRef, ...infoWindowProps} = this.props;
const infoWindow = InfoWindowCreator._createInfoWindow(mapHolderRef, infoWindowProps, anchorHolderRef);

Expand Down
9 changes: 8 additions & 1 deletion src/Marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as MarkerCreator,
markerDefaultPropTypes,
Expand Down Expand Up @@ -59,7 +63,10 @@ export default class Marker extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...markerProps} = this.props;
const marker = MarkerCreator._createMarker(mapHolderRef, markerProps);

Expand Down
9 changes: 8 additions & 1 deletion src/OverlayView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as OverlayViewCreator,
overlayViewDefaultPropTypes,
Expand Down Expand Up @@ -47,7 +51,10 @@ export default class OverlayView extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...overlayViewProps} = this.props;
const overlayView = OverlayViewCreator._createOverlayView(mapHolderRef, overlayViewProps);

Expand Down
9 changes: 8 additions & 1 deletion src/Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as PolygonCreator,
polygonDefaultPropTypes,
Expand Down Expand Up @@ -41,7 +45,10 @@ export default class Polygon extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...polygonProps} = this.props;
const polygon = PolygonCreator._createPolygon(mapHolderRef, polygonProps);

Expand Down
9 changes: 8 additions & 1 deletion src/Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as PolylineCreator,
polylineDefaultPropTypes,
Expand Down Expand Up @@ -39,7 +43,10 @@ export default class Polyline extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...polylineProps} = this.props;
const polyline = PolylineCreator._createPolyline(mapHolderRef, polylineProps);

Expand Down
9 changes: 8 additions & 1 deletion src/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as RectangleCreator,
rectangleDefaultPropTypes,
Expand Down Expand Up @@ -43,7 +47,10 @@ export default class Rectangle extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...rectangleProps} = this.props;
const rectangle = RectangleCreator._createRectangle(mapHolderRef, rectangleProps);

Expand Down
9 changes: 8 additions & 1 deletion src/SearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as SearchBoxCreator,
searchBoxDefaultPropTypes,
Expand Down Expand Up @@ -38,7 +42,10 @@ export default class SearchBox extends Component {

state = {}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, classes, style, ...searchBoxProps} = this.props;

// Cannot create input via component - Google Maps will mess with React's internal state by detaching/attaching.
Expand Down
9 changes: 8 additions & 1 deletion src/_Skeleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as SkeletonCreator,
skeletonDefaultPropTypes,
Expand Down Expand Up @@ -33,7 +37,10 @@ export default class Skeleton extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...skeletonProps} = this.props;
const skeleton = SkeletonCreator._createSkeleton(mapHolderRef, skeletonProps);

Expand Down
9 changes: 8 additions & 1 deletion src/addons/InfoBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
Component,
} from "react";

import {
canUseDOM,
} from "fbjs/lib/ExecutionEnvironment";

import {
default as InfoBoxCreator,
infoBoxDefaultPropTypes,
Expand Down Expand Up @@ -41,7 +45,10 @@ export default class InfoBox extends Component {
state = {
}

componentDidMount () {
componentWillMount () {
if (!canUseDOM) {
return;
}
const {mapHolderRef, ...infoBoxProps} = this.props;
const infoBox = InfoBoxCreator._createInfoBox(mapHolderRef, infoBoxProps);

Expand Down

3 comments on commit 91d5790

@idolize
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomchentw fbjs is discouraged from being used outside of Facebook. If all you want is canUseDOM why not include a public lib like can-use-dom?

@tomchentw
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@idolize I didn't notice that. Thanks for letting me know.

@tomchentw
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#134 created.

Please sign in to comment.