@nx/react:hook

Create a React component using Hooks in a dedicated React project.

Usage

1nx generate hook ... 2
1nx g c ... #same 2

By default, Nx will search for hook in the default collection provisioned in workspace.json.

You can specify the collection explicitly as follows:

1nx g @nx/react:hook ... 2

Show what will be generated without writing to disk:

1nx g hook ... --dry-run 2

Examples

Generate a hook with the exported symbol matching the file name. It results in the hook useFoo at mylib/src/lib/foo.ts:

1nx g @nx/react:hook mylib/src/lib/foo.ts 2

Generate a hook with the exported symbol different from the file name. It results in the hook useCustom at mylib/src/lib/foo.ts:

1nx g @nx/react:hook mylib/src/lib/foo.ts --name=useCustom 2

Generate a hook without providing the file extension. It results in the hook useFoo at mylib/src/lib/foo.ts:

1nx g @nx/react:hook mylib/src/lib/foo 2

Options

path

Required
string

The file path to the hook. Relative to the current working directory.

export

e
boolean
Default: false

When true, the hook is exported from the project index.ts (if it exists).

name

string

The hook symbol name. Defaults to the last segment of the file path.

skipTests

Internal
boolean
Default: false

When true, does not create spec.ts test files for the new hook.

js

Deprecated
boolean

Generate JavaScript files rather than TypeScript files.

Provide the full file path including the file extension in the path option. This option will be removed in Nx v21.