MSH

Understanding React Fundamentals

Published on

React is a JavaScript library for building user interfaces, particularly single-page applications. It allows you to create reusable UI components.

What is React?

React was developed by Facebook. It follows a component-based architecture and uses a virtual DOM for efficient rendering.

Key Features:

  • Declarative UI development
  • Component-based architecture
  • Virtual DOM for performance
  • Rich ecosystem of tools and libraries
  • Strong community support

Components

Components are the building blocks of React applications. They are reusable pieces of UI that can be composed together to create complex interfaces.

Function Components

Function components are the simplest way to define components in React. They are functions that return JSX.

1function Welcome(props) { 2 return <h1>Hello, {props.name}</h1>; 3}

State and Props

Props

Props are read-only properties passed from parent to child components:

1function UserCard({ name, email }) { 2 return ( 3 <div> 4 <h2>{name}</h2> 5 <p>{email}</p> 6 </div> 7 ); 8}

State

State is mutable data that can change over time:

1function Counter() { 2 const [count, setCount] = useState(0); 3 4 return ( 5 <div> 6 <p>Count: {count}</p> 7 <button onClick={() => setCount(count + 1)}>Increment</button> 8 </div> 9 ); 10 11}

Hooks

Hooks are functions that allow you to use state and other React features in function components.

useState

1const [state, setState] = useState(initialState); 2 3// Update the state setState(newState); 4// pass the new state to the function 5setState((prevState) => prevState + 1); // pass a function that takes the previous state and returns the new state

useEffect

1useEffect(() => { 2 // This will run when the component mounts and when the dependencies change 3 4 return () => { 5 // This will run when the component unmounts 6 // or when the dependencies change 7 }; 8 9}, [dependencies]); // Specify dependencies to trigger the effect when the dependencies change

useRef

1const ref = useRef(initialValue); 2 3// ref will allow us to access the input element 4// ref.current.value will give us the value of the input 5 6<input ref={ref} /> 7 8<input 9 ref={(element) => { 10 // element is the input element 11 // we can access the input element here 12 // we can also update the input element here 13 }} 14/>

Virtual DOM

React uses a virtual DOM to optimize rendering performance:

  1. When state changes, React creates a new virtual DOM tree
  2. Compares it with the previous virtual DOM tree
  3. Updates only the changed elements in the real DOM

This process is called reconciliation and helps maintain high performance even with frequent updates.

Key Takeaways

  1. Component Architecture

    • Reusable UI components
    • Clear separation of concerns
    • Easy to maintain and test
    • Promotes code reuse
  2. State Management

    • Props for parent-child communication
    • State for component data
    • Hooks for functional components
    • Context for global state
  3. Performance Optimization

    • Virtual DOM for efficient updates
    • Memoization for expensive computations
    • Code splitting for better load times
    • Lazy loading for components

References

GET IN TOUCH

Let's work together

I build exceptional and accessible digital experiences for the web

WRITE AN EMAIL

or reach out directly at hello@mohammadshehadeh.com