Skip to content

Commit

Permalink
Improve performance with useMemo (#872)
Browse files Browse the repository at this point in the history
this PR prevents unnecessary rerenders caused by setting states inside
objects therefore triggering rerender even f the value didn't change.

### How Has This Been Tested: 

Run test application and see if it works as expected.
  • Loading branch information
filip131311 authored Dec 22, 2024
1 parent 3838dc8 commit 2ae1cbe
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useCallback, useEffect, useState } from "react";
import React, { createContext, useCallback, useEffect, useMemo, useState } from "react";
import Alert from "../components/shared/Alert";

interface AlertType {
Expand Down Expand Up @@ -46,8 +46,12 @@ export default function AlertProvider({ children }: { children: React.ReactNode

const topAlert = getTopAlert(alerts);

const contextValue = useMemo(() => {
return { openAlert, isOpen, closeAlert };
}, [openAlert, isOpen, closeAlert]);

return (
<AlertContext.Provider value={{ openAlert, isOpen, closeAlert }}>
<AlertContext.Provider value={contextValue}>
{children}
<Alert
open={Boolean(topAlert?.id)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { makeProxy } from "../utilities/rpc";
Expand Down Expand Up @@ -74,15 +75,16 @@ export default function DependenciesProvider({ children }: PropsWithChildren) {
};
}, []);

const contextValue = useMemo(() => {
return {
dependencies: depsState,
runDiagnostics,
errors: getErrors(depsState),
};
}, [depsState, runDiagnostics, getErrors]);

return (
<DependenciesContext.Provider
value={{
dependencies: depsState,
runDiagnostics,
errors: getErrors(depsState),
}}>
{children}
</DependenciesContext.Provider>
<DependenciesContext.Provider value={contextValue}>{children}</DependenciesContext.Provider>
);
}

Expand Down
26 changes: 13 additions & 13 deletions packages/vscode-extension/src/webview/providers/DevicesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import {
Expand Down Expand Up @@ -64,19 +65,18 @@ export default function DevicesProvider({ children }: PropsWithChildren) {
};
}, []);

return (
<DevicesContext.Provider
value={{
devices,
finishedInitialLoad,
androidImages,
iOSRuntimes,
reload,
deviceManager: DeviceManager,
}}>
{children}
</DevicesContext.Provider>
);
const contextValue = useMemo(() => {
return {
devices,
finishedInitialLoad,
androidImages,
iOSRuntimes,
reload,
deviceManager: DeviceManager,
};
}, [devices, finishedInitialLoad, androidImages, iOSRuntimes, reload, DeviceManager]);

return <DevicesContext.Provider value={contextValue}>{children}</DevicesContext.Provider>;
}

export function useDevices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import {
Expand Down Expand Up @@ -57,10 +58,12 @@ export default function LaunchConfigProvider({ children }: PropsWithChildren) {
[config, setConfig]
);

const contextValue = useMemo(() => {
return { ...config, update, xcodeSchemes };
}, [config, update, xcodeSchemes]);

return (
<LaunchConfigContext.Provider value={{ ...config, update, xcodeSchemes }}>
{children}
</LaunchConfigContext.Provider>
<LaunchConfigContext.Provider value={contextValue}>{children}</LaunchConfigContext.Provider>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropsWithChildren, useContext, createContext, useState, useEffect } from "react";
import { PropsWithChildren, useContext, createContext, useState, useEffect, useMemo } from "react";
import { makeProxy } from "../utilities/rpc";
import { DeviceSettings, ProjectInterface, ProjectState } from "../../common/Project";

Expand Down Expand Up @@ -61,11 +61,11 @@ export default function ProjectProvider({ children }: PropsWithChildren) {
};
}, []);

return (
<ProjectContext.Provider value={{ projectState, deviceSettings, project, hasActiveLicense }}>
{children}
</ProjectContext.Provider>
);
const contextValue = useMemo(() => {
return { projectState, deviceSettings, project, hasActiveLicense };
}, [projectState, deviceSettings, project, hasActiveLicense]);

return <ProjectContext.Provider value={contextValue}>{children}</ProjectContext.Provider>;
}

export function useProject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useState,
useEffect,
useCallback,
useMemo,
} from "react";
import { makeProxy } from "../utilities/rpc";
import { WorkspaceConfig, WorkspaceConfigProps } from "../../common/WorkspaceConfig";
Expand Down Expand Up @@ -49,8 +50,12 @@ export default function WorkspaceConfigProvider({ children }: PropsWithChildren)
[config, setConfig]
);

const contextValue = useMemo(() => {
return { ...config, update };
}, [config, update]);

return (
<WorkspaceConfigContext.Provider value={{ ...config, update }}>
<WorkspaceConfigContext.Provider value={contextValue}>
{children}
</WorkspaceConfigContext.Provider>
);
Expand Down

0 comments on commit 2ae1cbe

Please sign in to comment.