Skip to content

Commit f01be15

Browse files
committed
Fixed bugs caused by functional component refs
1 parent df1bc8b commit f01be15

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/components/main/CanvasComponentNew.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { useDrag, useDrop, DropTargetMonitor } from 'react-dnd';
33
import { ItemTypes } from '../../constants/ItemTypes';
44
import { stateContext } from '../../context/context';
55
import { updateInstance } from '../../helperFunctions/instances';
6+
import { ComponentInstance } from '../../interfaces/interfacesNew';
67

7-
const CanvasComponent = (props):JSX.Element => {
8-
const ref = useRef<HTMLDivElement>(null);
8+
9+
const CanvasComponent = ({
10+
id,
11+
style,
12+
children
13+
}: ComponentInstance): JSX.Element => {
14+
const ref = useRef(null);
915
const pageId = 1;
1016

1117
const [context, setContext] = useContext(stateContext);
@@ -18,7 +24,7 @@ const CanvasComponent = (props):JSX.Element => {
1824
if (didDrop) {
1925
return;
2026
}
21-
const hoverId = props.id;
27+
const hoverId = id;
2228
// updates state with new instance
2329
setContext(updateInstance(hoverId, item, context, pageId));
2430
},
@@ -32,7 +38,7 @@ const CanvasComponent = (props):JSX.Element => {
3238
item: {
3339
type: ItemTypes.INSTANCE,
3440
newInstance: false,
35-
id: props.id
41+
id: id
3642
},
3743
// canDrag: !props.children.length,
3844
collect: (monitor: any) => ({
@@ -53,9 +59,9 @@ const CanvasComponent = (props):JSX.Element => {
5359
return (
5460
<div ref={ref} className="componentDefault" style={elementStyle}>
5561
I am a Canvas Component! :D
56-
{props.children}
62+
{children}
5763
</div>
5864
);
59-
}
65+
};
6066

6167
export default CanvasComponent;

src/components/main/MainCanvasNew.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const renderCanvas = (state, page) => {
2424
// renders an instance in the instance tree. If instance has children, those instances will also be rendered recursively
2525
const renderComponent = component => {
2626
const { id, style } = component;
27-
27+
2828
// const style = { border: '5px solid pink', margin: '40px' };
2929
return (
3030
<CanvasComponent id={id} style={style} key={id}>

src/helperFunctions/instances.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const componentInstanceGenerator = (newComponentData: NewComponentInstance) => {
99
// if component already exists then reuse the existing ID
1010
// otherwise, create a new ID
1111
// TODO: pull the nextId value from context
12-
console.log('In the new component generator');
12+
1313
let id;
1414
if (newComponentData.newInstance) {
1515
id = Math.floor(1000 * Math.random());
@@ -46,7 +46,9 @@ const findInstance = (
4646
// if so, return the object
4747
if (currentNode.id === instanceId) return currentNode;
4848
// add each of the objects children to the search array
49-
currentNode.children.forEach((node:ComponentInstance) => nodeArr.push(node));
49+
currentNode.children.forEach((node: ComponentInstance) =>
50+
nodeArr.push(node)
51+
);
5052
}
5153
// if no search is found return -1
5254
return -1;
@@ -100,25 +102,20 @@ const updateInstance = (
100102
instanceState: Context,
101103
pageNum: Number
102104
) => {
103-
console.log('newComponentData ', newComponentData);
104-
console.log('IN UPDATE INSTANCE');
105105
// copy state so we don't directly mutate state
106106
const state = { ...instanceState };
107107
// find instance heirarchy of the current page
108108
const currentPage = state.pages.find(page => page.pageId === pageNum);
109109
// create a new instance object w/ newComponentData. This is the component we're going to add to state
110110
// if we're going to move an existing instance
111-
const newComponent = componentInstanceGenerator(
112-
newComponentData
113-
);
111+
const newComponent = componentInstanceGenerator(newComponentData);
114112
// if moved component isn't a new isntance, delete the original istance
115113
if (!newComponentData.newInstance)
116114
deleteInstance(newComponentData.id, currentPage);
117115
// find the new parent for the new component
118116
const newParent = findInstance(newParentId, currentPage);
119117
// add new component to its new parent
120118
newParent.children.push(newComponent);
121-
console.log('New state is ', state);
122119
// return updated state
123120
return state;
124121
};

src/interfaces/InterfacesNew.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface NewComponentInstance {
66

77
export interface ComponentInstance {
88
id: Number;
9-
styling: Object;
9+
style: Object;
1010
children: ComponentInstance[];
1111
}
1212

0 commit comments

Comments
 (0)