React 18 useId 唯一 ID 生成 is becoming increasingly widespread in frontend development. This article dives into its core principles and best practices from real projects.
Basic Usage
The key lies in understanding the core logic:
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
Performance optimization should be tailored to specific scenarios; not all cases require over-optimization.
Advanced Usage
We can improve it in the following ways:
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
This approach has been running stably in production for over six months and has been practically validated.
Practical Cases
Let's start with the basic implementation:
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
This code demonstrates the basic usage. In real projects, you also need to consider error handling and edge cases.
Performance Optimization
Building on this foundation, we can further optimize:
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
This pattern is very practical in large projects and can significantly reduce maintenance costs.
Summary
- In team collaboration, conventions and documentation are more important than the technology itself
- Stay updated with the community; technical solutions need continuous iteration
- Don't adopt new technology just for the sake of it
- Code examples are for reference only and need to be adjusted according to your business scenario