DEV Community

Cover image for 12 Minutes to Master React: All the Concepts You Need to Know
PRANKUR PANDEY
PRANKUR PANDEY

Posted on

12 Minutes to Master React: All the Concepts You Need to Know

Ever felt overwhelmed by the jargon in React?

Terms like reconciliation, composition, and error boundaries can sound like a foreign language. Don’t worry—let’s demystify React together. Whether you're just starting out or need a refresher, this guide will break down React's core concepts in a way that's easy to understand.


Intro: The React Wonderland

React is a powerhouse JavaScript library with a lot of fancy terminology. But what do these terms really mean? We’ll start at the very beginning and build up our React knowledge step by step.

Image description---

Components: The Building Blocks

Think of components as the LEGO pieces of your React app. These are the fundamental building blocks that make up everything visible, from buttons to entire pages.

Here’s a simple React component:



function Button() {
  return <button>Click Me!</button>;
}


Enter fullscreen mode Exit fullscreen mode

JSX: JavaScript in Disguise

React components don’t return plain HTML; they return JSX, which stands for JavaScript XML. JSX is a syntax extension that looks like HTML but is actually JavaScript in disguise.



const App = () => (
  <div>
    <h1>Hello, World!</h1>
    <p>This is JSX!</p>
  </div>
);


Enter fullscreen mode Exit fullscreen mode

Image description


Curly Braces: Dynamic Magic

One of React’s magic tricks is the use of curly braces {} in JSX. You can insert dynamic JavaScript values directly into your JSX.



const name = "John";
return <h1>Hello, {name}!</h1>;


Enter fullscreen mode Exit fullscreen mode

Image description


Fragments: Avoiding Extra Elements

In React, a component can only return one parent element. If you need multiple elements, you can wrap them in a <div>. To avoid unnecessary HTML elements, use React Fragments (<></>).



return (
  <>
    <h1>Title</h1>
    <p>Description</p>
  </>
);


Enter fullscreen mode Exit fullscreen mode

Image description


Props: Passing Data Like a Pro

Props allow you to pass data to components, making them dynamic and reusable.



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

<Welcome name="Alice" />;


Enter fullscreen mode Exit fullscreen mode

Image description


Children: Components Inside Components

You can pass other components as props using the children prop.



function Wrapper({ children }) {
  return <div>{children}</div>;
}

<Wrapper>
  <h1>Nested Content</h1>
</Wrapper>;


Enter fullscreen mode Exit fullscreen mode

Image description


Keys: Unique Identifiers

When rendering lists, React needs to uniquely identify each item using the key prop.



const items = ['Apple', 'Banana', 'Cherry'];
return (
  <ul>
    {items.map((item, index) => (
      <li key={index}>{item}</li>
    ))}
  </ul>
);


Enter fullscreen mode Exit fullscreen mode

Image description


Rendering: Making Your Code Visible

Rendering turns your React code into a viewable app using the Virtual DOM, which updates only what’s necessary.

Image description


Event Handling: Reacting to User Actions

React handles user interactions through events like onClick and onChange.



function handleClick() {
  alert('Button clicked!');
}

return <button onClick={handleClick}>Click Me!</button>;


Enter fullscreen mode Exit fullscreen mode

Image description


State: Keeping Track of Changes

State allows React to track changes in your app. You can manage state using the useState hook.



const [count, setCount] = useState(0);

return (
  <div>
    <p>You clicked {count} times</p>
    <button onClick={() => setCount(count + 1)}>Click me</button>
  </div>
);


Enter fullscreen mode Exit fullscreen mode

Image description


Controlled Components: Predictable Behavior

Controlled components ensure form elements’ values are managed by React state.



const [value, setValue] = useState('');

return <input value={value} onChange={(e) => setValue(e.target.value)} />;


Enter fullscreen mode Exit fullscreen mode

Image description


Hooks: The Power of Function Components

Hooks like useState, useEffect, and useRef provide state management and side effects in functional components.



useEffect(() => {
  console.log("Component Mounted");
}, []);


Enter fullscreen mode Exit fullscreen mode

Image description


Purity: Consistent Outputs

A pure component always returns the same output for the same input. This predictability ensures fewer bugs.



function PureComponent({ name }) {
  return <h1>Hello, {name}</h1>;
}


Enter fullscreen mode Exit fullscreen mode

Strict Mode: Catching Errors Early

React’s Strict Mode helps you catch potential problems early in the development cycle.



<React.StrictMode>
  <App />
</React.StrictMode>;


Enter fullscreen mode Exit fullscreen mode

Effects: Interacting with the Outside World

The useEffect hook lets you handle side effects like data fetching.



useEffect(() => {
  fetch("/api/data")
    .then(res => res.json())
    .then(data => setData(data));
}, []);


Enter fullscreen mode Exit fullscreen mode

Image description


Refs: Direct DOM Access

Refs let you interact directly with DOM elements when necessary.



const inputRef = useRef();

function focusInput() {
  inputRef.current.focus();
}

return <input ref={inputRef} />;


Enter fullscreen mode Exit fullscreen mode

Context: Sharing Data Across Components

React Context provides a way to share data across components without prop drilling.



const UserContext = React.createContext();

<UserContext.Provider value={user}>
  <Component />
</UserContext.Provider>;


Enter fullscreen mode Exit fullscreen mode

Portals: Rendering Outside the DOM Hierarchy

Portals let you render components outside of the parent component's hierarchy.



ReactDOM.createPortal(
  <div>Modal Content</div>,
  document.getElementById('modal-root')
);


Enter fullscreen mode Exit fullscreen mode

Suspense: Handling Asynchronous Data

Suspense helps in rendering fallback UI while waiting for async data to load.



<Suspense fallback={<div>Loading...</div>}>
  <AsyncComponent />
</Suspense>;


Enter fullscreen mode Exit fullscreen mode

Error Boundaries: Graceful Error Handling

Error Boundaries catch JavaScript errors and display fallback UIs, preventing app crashes.



class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError() {
    return { hasError: true };
  }

  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}


Enter fullscreen mode Exit fullscreen mode

By understanding these core concepts, you can dive deeper into the React ecosystem and build sophisticated web applications!

Conclusions
Hope I cleared all your doubts about the React JS and its basic principles,Tell in comments which you liked the most.
You can connect with me here: LINKEDIN
TWITTER

Top comments (0)