JavaScript Frameworks: Introduction and Practice

introduction

As a powerful scripting language, JavaScript is widely used in front-end development. In order to improve development efficiency and code quality, many JavaScript frameworks have emerged. This blog will introduce the concept of JavaScript framework, discuss its advantages, and show its usage and effect through practical examples.

What are JavaScript frameworks?

A JavaScript framework is a collection of pre-written, reusable JavaScript code designed to simplify and speed up the development process. They provide some common functions and components, enabling developers to focus more on business logic without having to write code from scratch. Common JavaScript frameworks include React, Angular, and Vue.js, among others.

Advantages of JavaScript Frameworks

  1. Improve development efficiency: The framework provides a large number of ready-made components and tools, enabling developers to quickly build feature-rich applications. By using the framework, developers can reduce repetitive work and save development time.
  2. Improve code quality: The framework emphasizes code modularity and reusability, making code easier to maintain and expand. The framework also provides some conventions and best practices to help developers write more readable and maintainable code.
  3. Cross-platform support: Many JavaScript frameworks can be used on multiple platforms, including web, mobile, and desktop applications. This enables developers to use the same set of codes to build cross-platform applications, improving development efficiency and code reusability.

Hands-on example: Building a to-do list app with React

Below we will use the React framework to build a simple to-do list application. The app will have functionality to add, delete and mark as done.
First, we need to install the React framework and related dependencies. Open a terminal and execute the following command:

npm install react react-dom

Next, create a new React component TodoAppand import the required modules:

import React, {
    
     useState } from 'react';
const TodoApp = () => {
    
    
  const [todos, setTodos] = useState([]);
  const [newTodo, setNewTodo] = useState('');
  const handleInputChange = event => {
    
    
    setNewTodo(event.target.value);
  };
  const handleAddTodo = () => {
    
    
    if (newTodo.trim() !== '') {
    
    
      setTodos([...todos, newTodo]);
      setNewTodo('');
    }
  };
  const handleDeleteTodo = index => {
    
    
    const updatedTodos = todos.filter((_, i) => i !== index);
    setTodos(updatedTodos);
  };
  const handleToggleTodo = index => {
    
    
    const updatedTodos = todos.map((todo, i) => {
    
    
      if (i === index) {
    
    
        return {
    
     ...todo, completed: !todo.completed };
      }
      return todo;
    });
    setTodos(updatedTodos);
  };
  return (
    <div>
      <input type="text" value={
    
    newTodo} onChange={
    
    handleInputChange} />
      <button onClick={
    
    handleAddTodo}>Add</button>
      <ul>
        {
    
    todos.map((todo, index) => (
          <li
            key={
    
    index}
            style={
    
    {
    
     textDecoration: todo.completed ? 'line-through' : 'none' }}
          >
            {
    
    todo.text}
            <button onClick={
    
    () => handleDeleteTodo(index)}>Delete</button>
            <button onClick={
    
    () => handleToggleTodo(index)}>
              {
    
    todo.completed ? 'Undo' : 'Complete'}
            </button>
          </li>
        ))}
      </ul>
    </div>
  );
};
export default TodoApp;

We define a TodoAppcomponent that contains an input box, add button, to-do list and related action buttons. Use React's useStatehooks to manage the state of components, and handle user actions through event handlers.
Finally, introduce the component in the application's entry file TodoAppand render it into the DOM:

import React from 'react';
import ReactDOM from 'react-dom';
import TodoApp from './TodoApp';
ReactDOM.render(
  <React.StrictMode>
    <TodoApp />
  </React.StrictMode>,
  document.getElementById('root')
);

Through the above steps, we have successfully built a simple to-do list application using the React framework. Now, you can try running the app in your browser and experience its functionality.

Summarize

JavaScript frameworks are powerful tools for developing modern web applications. They provide a wealth of features and tools to help developers improve development efficiency, code quality, and cross-platform support. Through practical examples, we show how to build a to-do list application using the React framework. I hope this blog can help you better understand and apply JavaScript frameworks.
Reference link:

Guess you like

Origin blog.csdn.net/weixin_46254812/article/details/132241579