The culminating moment of our cosmic expedition has arrived. Throughout this entire module, we have been learning advanced patterns and best practices in React - from custom hooks, through Higher-Order Components and render props, to compound components, testing, and deployment. Now it is time to combine all these elements into one cohesive project: Space Mission Command Center.
When building a production application, it is crucial to think through its architecture. Our project combines the patterns we learned in this module:
1// Project structure
2// src/
3// hooks/ - Custom hooks (useMission, useFuel, useCrewStatus)
4// components/ - Reusable components
5// hoc/ - Higher-Order Components (withAuth, withLogging)
6// context/ - React Contexts (MissionContext, ThemeContext)
7// utils/ - Utility functions and validation
8// App.jsx - Main application componentCustom hooks allow you to extract logic from components and make it reusable. In our project, each aspect of the space mission has its own dedicated hook:
1// Custom hook for mission management
2function useMission(initialMission) {
3 const [mission, setMission] = useState(initialMission);
4 const [status, setStatus] = useState('planning');
5
6 const launch = () => {
7 if (mission.fuel >= mission.requiredFuel) {
8 setStatus('in-progress');
9 }
10 };
11
12 const complete = () => setStatus('completed');
13 const abort = () => setStatus('aborted');
14
15 return { mission, status, launch, complete, abort };
16}HOCs allow adding functionality to components without modifying their source code. Render props provide flexibility in rendering:
1// HOC - adds action logging
2function withLogging(WrappedComponent) {
3 return function LoggedComponent(props) {
4 const logAction = (action) => {
5 console.log(`[LOG] ${new Date().toISOString()}: ${action}`);
6 };
7 return <WrappedComponent {...props} logAction={logAction} />;
8 };
9}
10
11// Render Props - provides fuel data
12function FuelProvider({ render }) {
13 const [fuel, setFuel] = useState(100);
14 const consume = (amount) => setFuel(prev => Math.max(0, prev - amount));
15 const refuel = (amount) => setFuel(prev => Math.min(100, prev + amount));
16 return render({ fuel, consume, refuel });
17}The compound components pattern allows creating components that work together as a cohesive system:
1// Compound Component - Mission Panel
2function MissionPanel({ children }) {
3 const [isExpanded, setIsExpanded] = useState(false);
4 return (
5 <div className="mission-panel">
6 {React.Children.map(children, child =>
7 React.isValidElement(child)
8 ? React.cloneElement(child, { isExpanded, setIsExpanded })
9 : child
10 )}
11 </div>
12 );
13}
14
15MissionPanel.Header = function Header({ isExpanded, setIsExpanded, children }) {
16 return (
17 <div onClick={() => setIsExpanded(!isExpanded)} style={{ cursor: 'pointer' }}>
18 {children} {isExpanded ? '▲' : '▼'}
19 </div>
20 );
21};
22
23MissionPanel.Body = function Body({ isExpanded, children }) {
24 return isExpanded ? <div>{children}</div> : null;
25};Production code must be tested. Every custom hook, component, and utility function should have appropriate tests:
1// Example custom hook test
2// describe('useMission', () => {
3// it('should launch the mission when there is enough fuel', () => {
4// const { result } = renderHook(() =>
5// useMission({ fuel: 100, requiredFuel: 50 })
6// );
7// act(() => result.current.launch());
8// expect(result.current.status).toBe('in-progress');
9// });
10//
11// it('should not launch the mission without fuel', () => {
12// const { result } = renderHook(() =>
13// useMission({ fuel: 20, requiredFuel: 50 })
14// );
15// act(() => result.current.launch());
16// expect(result.current.status).toBe('planning');
17// });
18// });Before deploying the application to production, make sure that:
React.memo, useMemo, and useCallback where neededlazy() and Suspense.env filesnpm run build and test the resultIn this module, you learned advanced React patterns that are the foundation of professional application development:
These patterns are your cosmic engineer toolkit - with them, you can build React applications at the highest level!