Vite
Install and configure reactuse for Vite.
Create project
Start by creating a new React project using vite. Select the React + TypeScript template:
bash
npm create vite@latestbash
yarn create vite@latestbash
pnpm create vite@latestbash
bun create vite@latestEdit tsconfig.json file
The current version of Vite splits TypeScript configuration into three files, two of which need to be edited. Add the baseUrl and paths properties to the compilerOptions section of the tsconfig.json and tsconfig.app.json files:
json
{
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.node.json"
}
],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}Edit tsconfig.app.json file
Add the following code to the tsconfig.app.json file to resolve paths, for your IDE:
json
{
"compilerOptions": {
// ...
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
// ...
}
}Update vite.config.ts
Add the following code to the vite.config.ts so your app can resolve paths without error:
bash
npm install -D @types/nodebash
yarn add -D @types/nodebash
pnpm add -D @types/nodebash
bun add -D @types/nodetypescript
import react from '@vitejs/plugin-react';
import path from 'node:path';
import { defineConfig } from 'vite';
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
}
});Run the CLI
Run the useverse init command to set up your project:
bash
npx useverse@latest initThis creates a reactuse.json config file in your project.
Add hooks
Add hooks with the CLI.
bash
npx useverse@latest add useBooleanThen import and use the hook in your app:
tsx
import { useBoolean } from '@/shared/hooks';
const App = () => {
const [on, toggle] = useBoolean();
return (
<div>
<button onClick={() => toggle()}>Click me</button>
<p>{on.toString()}</p>
</div>
);
};
export default App;