Home > Article > Web Front-end > Things I Wish I Knew When I Started with React
Lessons from 3 Years of React Development
When I first dove into React, it felt like opening Pandora's box. There was so much to learn, and along the way, I encountered plenty of "Aha!" moments. Here are 10 things I wish I knew when I started, to help you skip a few speed bumps in your React journey.
The most important realization? React components are just JavaScript functions. You pass props as arguments, and they return JSX, which looks like HTML but isn’t. Once you think of components this way, understanding concepts like props and state becomes much simpler.
const MyComponent = (props) => { return <h1>{props.title}</h1>; };
This might seem basic now, but early on, the difference between props and state wasn’t obvious to me. Here’s a quick refresher:
When in doubt: If the data comes from the parent, it's props. If the data lives inside the component, it’s state.
When React introduced hooks, it was a game-changer. Instead of juggling lifecycle methods, you can now manage state and side effects easily using hooks like useState and useEffect. I wish I had known how powerful and simple these hooks could make my code from the beginning.
const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; }, [count]);
I didn’t fully grasp how React’s Virtual DOM worked until later, and that was a mistake. React’s efficiency comes from updating the Virtual DOM instead of the real DOM directly. By diffing the changes, React only updates what’s necessary, making your apps fast.
React favors component composition (nesting components inside each other) rather than class-based inheritance. If you need to reuse logic across components, it's better to extract it into reusable components or custom hooks rather than using inheritance.
const Greeting = ({name}) => <h1>Hello, {name}!</h1>; const Page = () => <Greeting name="John" />;
As your app grows, state management becomes tricky. Local component state works well for smaller apps, but for larger ones, tools like Context API or libraries like Redux help manage state across your entire application. I started with Redux too early, but I now know when to lean on simpler solutions like useContext or useReducer before introducing heavier tools.
const MyContext = React.createContext(); const App = () => { return ( <MyContext.Provider value={/* some value */}> <ComponentA /> </MyContext.Provider> ); };
If you're working on larger codebases, adopting TypeScript is worth the learning curve. It can prevent bugs early by enforcing types, and it makes collaborating with other developers smoother. I struggled with TypeScript at first, but once I embraced it, my React code became much more robust.
interface Props { title: string; } const MyComponent: React.FC<Props> = ({ title }) => { return <h1>{title}</h1>; };
When I started, I wrestled with global styles clashing with each other. CSS-in-JS libraries like styled-components or Emotion changed the game for me by allowing scoped styles that live alongside the component logic.
import styled from 'styled-components'; const Button = styled.button` background: blue; color: white; padding: 10px; `; const App = () => <Button>Click Me</Button>;
Testing React components was intimidating, but tools like React Testing Library and Jest make it easy. Write tests for important components to ensure they behave as expected, and you’ll thank yourself later.
import { render } from '@testing-library/react'; import MyComponent from './MyComponent'; test('renders the title', () => { const { getByText } = render(<MyComponent title="Hello" />); expect(getByText(/Hello/i)).toBeInTheDocument(); });
As your app scales, you’ll want to optimize it for performance. Techniques like memoization (React.memo), lazy loading components (React.lazy), and splitting code (React.Suspense) can drastically improve the user experience. Performance wasn’t top of mind for me early on, but it should be for you!
const LazyComponent = React.lazy(() => import('./LazyComponent')); const App = () => ( <React.Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </React.Suspense> );
React is an amazing tool for building dynamic UIs, but like any technology, it comes with a learning curve. Embrace the basics, and don't shy away from exploring advanced concepts. Most importantly, keep building!
These are the 10 things I wish I knew from the start. Hopefully, they’ll save you some time and frustration on your React journey.
Did this help? Drop a comment or share your own React tips below!
The above is the detailed content of Things I Wish I Knew When I Started with React. For more information, please follow other related articles on the PHP Chinese website!