Node ID fixed with useRef
This commit is contained in:
parent
62d75a0446
commit
ae60f8ae96
2 changed files with 20 additions and 15 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState, useRef } from "react";
|
||||||
|
|
||||||
type flow={name:string,id:string,data:any}
|
type flow={name:string,id:string,data:any}
|
||||||
|
|
||||||
|
|
@ -9,7 +9,7 @@ type TabsContextType={
|
||||||
removeFlow:(id:string)=>void;
|
removeFlow:(id:string)=>void;
|
||||||
addFlow:(flowData?:any)=>void;
|
addFlow:(flowData?:any)=>void;
|
||||||
updateFlow:(newFlow:flow)=>void;
|
updateFlow:(newFlow:flow)=>void;
|
||||||
nodeId:number;
|
incrementNodeId:()=>number,
|
||||||
}
|
}
|
||||||
|
|
||||||
const TabsContextInitialValue = {
|
const TabsContextInitialValue = {
|
||||||
|
|
@ -19,31 +19,38 @@ const TabsContextInitialValue = {
|
||||||
removeFlow:(id:string)=>{},
|
removeFlow:(id:string)=>{},
|
||||||
addFlow:(flowData?:any)=>{},
|
addFlow:(flowData?:any)=>{},
|
||||||
updateFlow:(newFlow:flow)=>{},
|
updateFlow:(newFlow:flow)=>{},
|
||||||
nodeId:0,
|
incrementNodeId:()=>0,
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TabsContext = createContext<TabsContextType>(TabsContextInitialValue)
|
export const TabsContext = createContext<TabsContextType>(TabsContextInitialValue)
|
||||||
|
|
||||||
|
let _ = require('lodash');
|
||||||
|
|
||||||
export function TabsProvider({children}){
|
export function TabsProvider({children}){
|
||||||
const [tabIndex,setTabIndex] = useState(0)
|
const [tabIndex,setTabIndex] = useState(0)
|
||||||
const [flows,setFlows] = useState<Array<flow>>([])
|
const [flows,setFlows] = useState<Array<flow>>([])
|
||||||
const [id, setId] = useState(0);
|
const [id, setId] = useState(0);
|
||||||
let nodeId = 0;
|
|
||||||
|
const newNodeId = useRef(0);
|
||||||
|
function incrementNodeId(){
|
||||||
|
newNodeId.current = newNodeId.current + 1;
|
||||||
|
return newNodeId.current;
|
||||||
|
}
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if(flows.length !== 0)
|
if(flows.length !== 0)
|
||||||
window.localStorage.setItem('tabsData', JSON.stringify({tabIndex, flows, id, nodeId}));
|
window.localStorage.setItem('tabsData', JSON.stringify({tabIndex, flows, id, nodeId: newNodeId.current}));
|
||||||
}, [flows, id, nodeId, tabIndex]);
|
}, [flows, id, tabIndex, newNodeId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cookie = window.localStorage.getItem('tabsData');
|
let cookie = window.localStorage.getItem('tabsData');
|
||||||
if(cookie){
|
if(cookie){
|
||||||
let cookieObject = JSON.parse(cookie);
|
let cookieObject = JSON.parse(cookie);
|
||||||
setTabIndex(cookieObject.tabIndex);
|
setTabIndex(cookieObject.tabIndex);
|
||||||
setFlows(cookieObject.flows)
|
setFlows(cookieObject.flows);
|
||||||
setId(cookieObject.id)
|
setId(cookieObject.id);
|
||||||
nodeId = cookieObject.nodeId
|
newNodeId.current= cookieObject.nodeId;
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
@ -88,8 +95,8 @@ export function TabsProvider({children}){
|
||||||
}
|
}
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<TabsContext.Provider value={{tabIndex,setTabIndex,flows, nodeId,removeFlow,addFlow,updateFlow}}>
|
<TabsContext.Provider value={{tabIndex,setTabIndex,flows,incrementNodeId, removeFlow,addFlow,updateFlow}}>
|
||||||
{children}
|
{children}
|
||||||
</TabsContext.Provider>
|
</TabsContext.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,15 +32,13 @@ const nodeTypes = {
|
||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
export default function FlowPage({ flow }) {
|
export default function FlowPage({ flow }) {
|
||||||
let { updateFlow, nodeId } = useContext(TabsContext);
|
let { updateFlow, incrementNodeId } = useContext(TabsContext);
|
||||||
const { types, reactFlowInstance, setReactFlowInstance } =
|
const { types, reactFlowInstance, setReactFlowInstance } =
|
||||||
useContext(typesContext);
|
useContext(typesContext);
|
||||||
const reactFlowWrapper = useRef(null);
|
const reactFlowWrapper = useRef(null);
|
||||||
|
|
||||||
function getId(){
|
function getId(){
|
||||||
console.log(nodeId);
|
return `dndnode_}`+incrementNodeId();
|
||||||
nodeId = nodeId+1
|
|
||||||
return `dndnode_}`+nodeId;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const { setExtraComponent, setExtraNavigation } = useContext(locationContext);
|
const { setExtraComponent, setExtraNavigation } = useContext(locationContext);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue