Summary
Convert the remaining React class components to functional components using hooks. This modernises the codebase and provides several benefits.
What are Hooks?
Hooks (introduced in React 16.8, 2019) let you use state and lifecycle features in functional components instead of classes. They're now the recommended approach.
// Before: Class component
class EditorContainer extends Component {
constructor(props) {
super(props);
this.state = { authors: [] };
}
componentDidMount() {
this.loadAuthors();
}
render() {
return <Editor authors={this.state.authors} />;
}
}
// After: Functional component with hooks
function EditorContainer() {
const [authors, setAuthors] = useState([]);
useEffect(() => {
loadAuthors().then(setAuthors);
}, []);
return <Editor authors={authors} />;
}
Benefits
- Less code - Typically 30-50% fewer lines (no constructor, no
this binding, no lifecycle method boilerplate)
- Easier to understand - Logic is colocated instead of spread across lifecycle methods
- Reusable logic - Custom hooks can share stateful logic between components
- Better TypeScript support - Functional components have simpler type signatures
- Performance - Hooks enable fine-grained memoisation with
useMemo/useCallback
- Testing - Easier to test (no enzyme shallow rendering issues)
Drawbacks / Considerations
- Learning curve - Different mental model (effects vs lifecycles)
- Existing tests - May need updates if they rely on class instance methods
- Dependency arrays -
useEffect dependencies can be tricky to get right
- No
this - Can't access instance methods (but this is usually fine)
Components to Migrate
| Component |
Lines |
Complexity |
Notes |
AppContainer |
~80 |
Medium |
Entry point, config loading |
EditorContainer |
~350 |
High |
Most complex, many methods |
EntryContainer |
~100 |
Medium |
Entry display, edit mode |
PreviewContainer |
~50 |
Low |
Simple preview |
EventsContainer |
~60 |
Low |
Key events |
PaginationContainer |
~40 |
Low |
Load more |
Already using hooks: LexicalEditor (new implementation)
Approach
- Start with simplest components (
PaginationContainer, PreviewContainer)
- Create custom hooks for shared logic (
useEntries, usePolling)
- Migrate complex components last (
EditorContainer)
- Update tests as components are converted
References
Summary
Convert the remaining React class components to functional components using hooks. This modernises the codebase and provides several benefits.
What are Hooks?
Hooks (introduced in React 16.8, 2019) let you use state and lifecycle features in functional components instead of classes. They're now the recommended approach.
Benefits
thisbinding, no lifecycle method boilerplate)useMemo/useCallbackDrawbacks / Considerations
useEffectdependencies can be tricky to get rightthis- Can't access instance methods (but this is usually fine)Components to Migrate
AppContainerEditorContainerEntryContainerPreviewContainerEventsContainerPaginationContainerAlready using hooks:
LexicalEditor(new implementation)Approach
PaginationContainer,PreviewContainer)useEntries,usePolling)EditorContainer)References