Skip to content

Commit

Permalink
refactor(app): use memo fpr comps
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Jul 25, 2019
1 parent 3ecd00d commit bfeefd5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/EdgeRenderer/EdgeTypes/wrapEdge.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React, { useContext } from 'react';
import ReactDraggable from 'react-draggable';
import React, { useContext, memo } from 'react';
import cx from 'classnames';

import { GraphContext } from '../../GraphContext';
import { updateNodePos, setSelectedElements } from '../../state/actions';
import { setSelectedElements } from '../../state/actions';
import { isEdge } from '../../graph-utils';

const isInputTarget = (e) => ['INPUT', 'SELECT', 'TEXTAREA'].includes(e.target.nodeName);

export default EdgeComponent => (props) => {
export default EdgeComponent => memo((props) => {
const { state, dispatch } = useContext(GraphContext);
const { data, onClick } = props;
const selected = state.selectedElements
Expand All @@ -31,4 +30,4 @@ export default EdgeComponent => (props) => {
<EdgeComponent {...props} />
</g>
);
};
});
6 changes: 3 additions & 3 deletions src/GlobalKeyHandler/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useContext } from 'react';
import { useEffect, useContext, memo } from 'react';

import { useKeyPress } from '../hooks';
import { setNodesSelection } from '../state/actions';
import { GraphContext } from '../GraphContext';
import { isEdge, getConnectedEdges } from '../graph-utils';

export default (props) => {
export default memo((props) => {
const { state, dispatch } = useContext(GraphContext);
const removePressed = useKeyPress('Backspace');

Expand All @@ -25,4 +25,4 @@ export default (props) => {
}, [removePressed])

return null;
};
});
7 changes: 3 additions & 4 deletions src/GraphView/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useContext, useRef } from 'react';
import React, { useEffect, useContext, useRef, memo } from 'react';
import * as d3Zoom from 'd3-zoom';
import { select, event } from 'd3-selection';
import ReactSizeMe from 'react-sizeme';
Expand All @@ -13,7 +13,7 @@ import { useKeyPress } from '../hooks';

const d3ZoomInstance = d3Zoom.zoom().scaleExtent([0.5, 2]);

const GraphView = (props) => {
const GraphView = memo((props) => {
const zoomPane = useRef(null);
const { state, dispatch } = useContext(GraphContext);
const shiftPressed = useKeyPress('Shift');
Expand Down Expand Up @@ -88,7 +88,6 @@ const GraphView = (props) => {
/>
</div>
);
};

});

export default ReactSizeMe.withSize({ monitorHeight: true })(GraphView);
6 changes: 3 additions & 3 deletions src/NodeRenderer/NodeTypes/wrapNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useContext, useState } from 'react';
import React, { useEffect, useRef, useContext, useState, memo } from 'react';
import ReactDraggable from 'react-draggable';
import cx from 'classnames';

Expand All @@ -8,7 +8,7 @@ import { isEdge } from '../../graph-utils';

const isInputTarget = (e) => ['INPUT', 'SELECT', 'TEXTAREA'].includes(e.target.nodeName);

export default NodeComponent => (props) => {
export default NodeComponent => memo((props) => {
const nodeElement = useRef(null);
const { state, dispatch } = useContext(GraphContext);
const [offset, setOffset] = useState({ x: 0, y: 0 });
Expand Down Expand Up @@ -81,4 +81,4 @@ export default NodeComponent => (props) => {
</div>
</ReactDraggable.DraggableCore>
);
};
});
6 changes: 3 additions & 3 deletions src/NodesSelection/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react';
import React, { useContext, useState, memo } from 'react';
import ReactDraggable from 'react-draggable';

import { GraphContext } from '../GraphContext';
Expand All @@ -20,7 +20,7 @@ function getStartPositions(elements) {
}, {});
}

export default () => {
export default memo(() => {
const graphContext = useContext(GraphContext);
const [offset, setOffset] = useState({ x: 0, y: 0 });
const [startPositions, setStartPositions] = useState({});
Expand Down Expand Up @@ -79,4 +79,4 @@ export default () => {
</ReactDraggable>
</div>
);
};
});
6 changes: 3 additions & 3 deletions src/UserSelection/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState, useContext } from 'react';
import React, { useEffect, useRef, useState, useContext, memo } from 'react';

import { GraphContext } from '../GraphContext';
import { updateSelection, setSelection, setNodesSelection } from '../state/actions';
Expand All @@ -22,7 +22,7 @@ function getMousePosition(evt) {
};
}

export default () => {
export default memo(() => {
const selectionPane = useRef(null);
const [rect, setRect] = useState(initialRect);
const { dispatch } = useContext(GraphContext);
Expand Down Expand Up @@ -106,4 +106,4 @@ export default () => {
)}
</div>
);
}
});

0 comments on commit bfeefd5

Please sign in to comment.