search
HomeWeb Front-endJS TutorialFundamental Core Concepts of React

In the fast-evolving world of web development, React remains a cornerstone for building dynamic and performant user interfaces. Whether you're a seasoned developer or just starting out, understanding React's core concepts is essential for leveraging its full potential. In this article, we'll explore the fundamental principles of React, from its library status to the power of hooks, and provide you with a clear foundation to enhance your React skills. Let’s dive in! ?

1. Is React a Framework or Library?

React is a JavaScript library, not a framework. Unlike frameworks, which provide a comprehensive set of tools and enforce a particular way of building applications, React focuses on a specific aspect—UI rendering. This makes React highly flexible and popular, as it follows the Unix philosophy of doing one thing and doing it well.

2. Virtual DOM

The DOM stands for Document Object Model in simple words representing the UI of an application. Every time when we change the UI, the DOM gets updated to represent that change. The DOM is represented as a tree data structure. When we change the UI, the DOM re-render and updates its children. The re-rendering of the UI makes application slow.

For this solution we use Virtual DOM. The virtual DOM is only a virtual representation of the DOM. When the state of application changes, the Virtual DOM gets updated instead of the real DOM.

Virtual DOM every time creates a tree and elements are represented as a node. If any of the elements changes a new virtual DOM tree is created. The new tree is then compared or “diffed” with the previous tree.

Fundamental Core Concepts of React

In this image, the red circles represent the nodes that have changed. These nodes represent the UI elements that change state. Then compared the previous tree and current changed tree. Updated tree is then batch updated to the real DOM. This makes React standout as a high performance JavaScript library.

In summary:

  1. The entire virtual DOM gets updated.
  2. The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed.
  3. The changed objects, and the changed objects only, get updated on the real DOM.
  4. Changes on the real DOM cause the screen to change.

3. JSX

JSX (JavaScript XML) allows you to write HTML-like code in React. It converts HTML tags into React elements using React.createElement( component, props, …children) function.

For example:
JSX code:

<mytext color="red">
  Hello, Good Morning!
</mytext>

This example compile into:

React.createElement(
  MyText,
  { color: 'red' },
  'Hello, Good Morning!'
)

Note: User-defined components must start with a capital letter. Lowercase tags are treated as HTML elements.

4. Props in JSX

Props can be specified in several ways in JSX:

JavaScript Expressions as Props:

<sumcomponent sum="{1"></sumcomponent>

Here, props.sum evaluates to 6.

String Literals:

<textcomponent text="{‘Good" morning></textcomponent>

<textcomponent text="”Good" morning></textcomponent>

Both examples above are equivalent.

Props Default to “True”
If we do not pass a value for a prop, it defaults to true.

For example,

<textcomponent prop></textcomponent>

<textcomponent prop="{true}"></textcomponent>

Both examples above are equivalent.

5. Class Components

Components in React can be defined as classes or functions. Here's how to define a class component:

class Greetings extends React.Component {
  render() {
    return <h1 id="Hello-this-props-name">Hello, {this.props.name}</h1>;
  }
}

6. The Component Lifecycle

Components have lifecycle methods that you can override to run code at specific stages:

Mounting: When a component is being created and inserted into the DOM.

  • constructor()
  • render()
  • componentDidMount()

Updating: When props or state change.

  • render()
  • componentDidUpdate()

Unmounting: When a component is being removed from the DOM.

  • componentWillUnmount()

7. Class Properties

defaultProps allows you to define default values for props:

class MyText extends React.Component {
  // Component code here
}

MyText.defaultProps = {
  color: 'gray'
};

If props.color is not provided, it defaults to 'gray'.

8. Prop-Types

We can use prop-types for checking types of passed properties of components. It gives errors, when they don’t match.

import PropTypes from 'prop-types';

const studentPropTypes = {
  studentName: PropTypes.string,
  id: PropTypes.number
};

const props = {
  studentName: 'Asima',
  id: 'hi' // Invalid
};

PropTypes.checkPropTypes(studentPropTypes, props, 'prop', 'MyComponent');

This will warn about the type mismatch for id.

9. Optimizing Performance

React is designed for performance, but you can further optimize it:

Using Production Build:

npm run build

This creates a production build with optimizations.

Minimize Source Code: Be cautious with changes to React's source code.

Code Splitting: Bundle JavaScript code into chunks to load as needed.

10. React Hooks

Hooks are functions that let you use state and other React features in function components. The two most popular hooks are:

useState: Adds state to function components.

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

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

useEffect: Manages side effects in function components.

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

  useEffect(() => {
    document.title = `You clicked ${count} times`;
  });

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

React has continually evolved to meet the demands of modern web development, and mastering its core concepts is crucial for building efficient, scalable applications. From understanding how React differentiates itself as a library to harnessing the power of hooks for functional components, these fundamentals will set you on the path to React proficiency.

As you continue to explore and implement React in your projects, remember that staying updated with the latest practices and features will keep you ahead in the ever-changing tech landscape. If you found this article valuable, don’t forget to give it a like and share it with fellow developers eager to deepen their React knowledge!

Thank you for reading, and happy coding! ?

The above is the detailed content of Fundamental Core Concepts of React. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor