Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- `useConstructor` hook that executes a provided callback only once during the component's lifecycle, similar to a constructor.
65 changes: 65 additions & 0 deletions cypress/component/useConstructor.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useState } from "react";
import { useConstructor } from "../../src/lib/hooks/useConstructor";

describe("useConstructor", () => {
it("calls the callback exactly once per mount, even after re-renders", () => {
const initSpy = cy.spy().as("initSpy");

const TestComponent: React.FC = () => {
const [count, setCount] = useState(0);
useConstructor(() => {
initSpy();
});

return (
<div>
<span data-cy="count">{count}</span>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
</div>
);
};

// Turn off StrictMode as it will double‑invoke mount logic
cy.mount(<TestComponent />, { strict: false });

cy.get("@initSpy").should("have.been.calledOnce");

// Cause several re-renders
cy.contains("Increment").click().click().click();
cy.get("[data-cy='count']").should("have.text", "3");

cy.get("@initSpy").should("have.been.calledOnce");
});

it("runs again after an unmount/remount (new instance)", () => {
const initSpy = cy.spy().as("initSpy");

const Wrapper: React.FC = () => {
const [show, setShow] = useState(true);
return (
<div>
<button onClick={() => setShow((s) => !s)}>Toggle</button>
{show && <Child />}
</div>
);
};

const Child: React.FC = () => {
useConstructor(() => initSpy());
return <div data-cy="child">Child</div>;
};

// Turn off StrictMode as it will double‑invoke mount logic
cy.mount(<Wrapper />, { strict: false });

cy.get("@initSpy").should("have.been.calledOnce");

cy.contains("Toggle").click(); // unmount
cy.get("[data-cy='child']").should("not.exist");

cy.contains("Toggle").click(); // remount
cy.get("[data-cy='child']").should("exist");

cy.get("@initSpy").should("have.callCount", 2);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./lib/hooks/useConstructor";
export * from "./lib/hooks/useHelloWorld";
15 changes: 15 additions & 0 deletions src/lib/hooks/useConstructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useRef } from "react";

/**
* Custom hook that runs the provided callback only once during the component's lifecycle.
* @param callback - Function to be executed once on component mount.
*/
const useConstructor = (callback: () => void) => {
const calledRef = useRef(false);
if (!calledRef.current) {
callback();
calledRef.current = true;
}
};

export { useConstructor };
Loading