Stock Trading - React

React View - Virtual DOM

React uses a Virtual DOM to efficiently update the UI.

Virtual DOM is a lightweight JavaScript representation of the real DOM. When a component's state changes, React:

  1. Renders a new Virtual DOM tree.

  2. Compares (diffs) it with the previous Virtual DOM tree.

  3. Calculates the minimal set of changes needed.

  4. Applies those changes to the real DOM.

Component Lifecycle

Most of us use Functional Components. Instead of multiple lifecycle methods, we use the useEffect Hook to handle all.

Lifecycle Phase Class Method Functional Hook equivalent
Mounting componentDidMount useEffect(() => { ... }, [])
Updating componentDidUpdate useEffect(() => { ... }, [dependency])
Unmounting componentWillUnmount useEffect(() => { return () => { ... } })

More details:

Class Component Method Functional Hook Equivalent When it runs
constructor useState initializer or useMemo/useRef During render, before first paint
getDerivedStateFromProps useState setter during render or useMemo During render, before first paint
render The function body itself Every render
componentDidMount useEffect(fn, []) After first paint (commit phase)
shouldComponentUpdate React.memo (for props), useMemo for values Before render, can skip re-rendering
getSnapshotBeforeUpdate useLayoutEffect with previous values capture Just before DOM updates are applied
componentDidUpdate useEffect(fn, [deps]) without cleanup After paint (if deps changed)
componentWillUnmount Cleanup function of useEffect(fn, []) Before unmount
componentDidCatch / getDerivedStateFromError No Hook equivalent (use a class error boundary) When an error is thrown in a descendant

React hooks

useState

  • Used to store and manage state in a functional component.

  • Returns an array of two items: the current state and a function to update it.

Example:

javascript 复制代码
import React, { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0); // count starts at 0

  const increment = () => {
    setCount(count + 1); // update the state
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

export default Counter;

Key points:

  • Calling setCount triggers a re-render.

  • The state persists across renders.

useEffect

  • Used for side effects: things like fetching data, subscriptions, timers.

  • Runs after the component renders.

Basic usage:

javascript 复制代码
import React, { useState, useEffect } from "react";

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    const interval = setInterval(() => {
      setSeconds(prev => prev + 1); // update every second
    }, 1000);

    // Cleanup function
    return () => clearInterval(interval);
  }, []); // empty dependency array → runs only once

  return <p>Seconds: {seconds}</p>;
}

export default Timer;

✅ Key points:

  • Dependency array ([]) controls when the effect runs:

    • [] → once on mount.

    • [count] → runs whenever count changes.

    • Omitted → runs after every render.

  • Can return a cleanup function to avoid memory leaks.


useState + useEffect together

They often pair to manage state reactively:

javascript 复制代码
import React, { useState, useEffect } from "react";

function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch("https://api.example.com/data")
      .then(res => res.json())
      .then(json => setData(json));
  }, []); // runs once on mount

  if (!data) return <p>Loading...</p>;
  return <pre>{JSON.stringify(data, null, 2)}</pre>;
}

export default DataFetcher;

Here's the flow:

  1. Component mounts → data is null.

  2. useEffect triggers the fetch → updates state.

  3. setData triggers re-render, showing the fetched data.

Redux - State

Three core principles:

  1. Single source of truth: The entire application state is stored in a single JavaScript object called the store.

  2. State is read-only: The only way to change the state is to emit an action, an object describing what happened.

  3. Changes are made with pure functions: To specify how the state tree is transformed by actions, you write reducers -- pure functions that take the previous state and an action, and return the next state.

What's Redux?

Use reducer to create/update/get store. Redux provides a predictable state container with a store, reducers, actions, and optional middleware for side effects.

Key concepts:

  • Store

The core of Redux is "store".

Key functions: createStore(reducers[, initialState]), getState, dispatch, subscribe, etc.

The store is an object that holds the application state. It provides a few essential methods:

getState() -- returns the current state.

dispatch(action) -- sends an action to the reducer to trigger a state update.

subscribe(listener) -- registers a callback that will be called whenever the state changes.

  • State

State is read-only, changes via pure functions (reducers).

  • Reducer

A reducer is a function with the signature (state, action) => newState. It must be a pure function -- no side effects, no mutation of arguments. Given the same inputs, it must always return the same output.

Example of a simple reducer:

复制代码
const initialState = { count: 0 };

function counterReducer(state = initialState, action) {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    case 'DECREMENT':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

When your app grows, you can split reducers into smaller pieces and combine them with combineReducers:

复制代码
import { combineReducers } from 'redux';

const rootReducer = combineReducers({
  counter: counterReducer,
  user: userReducer,
  // ...
});

combineReducers produces a single reducer that calls each child reducer and gathers their results into a single state object.

  • Action

An action is a plain JavaScript object that must have a type property (usually a string constant) indicating the type of action being performed. It may also contain additional data (payload) needed to update the state. Store dispatch an action to update state.

复制代码
const incrementAction = { type: 'INCREMENT' };
const addTodoAction = { type: 'ADD_TODO', payload: 'Learn Redux' };

Actions are created by action creators -- functions that return action objects:

复制代码
function increment() {
  return { type: 'INCREMENT' };
}

To update the state, you dispatch an action:

复制代码
store.dispatch(increment());
  • Middleware

Using middleware for async actions.

applyMiddleware, examples (redux-saga, redux-thunk).

Routing - React Router

Enables navigation between views.

AJAX request

fetch

Internationalization (i18n)

react-intj: a React library for internationalization (i18n),

相关推荐
月光宝盒造梦师2 分钟前
Ant Design Ellipsis 中的判断逻辑 isEleEllipsis 方法非常消耗性能
javascript·react·优化
酉鬼女又兒1 小时前
零基础快速入门前端ES6 核心特性详解:Set 数据结构与对象增强写法(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·es6
阿珊和她的猫2 小时前
以用户为中心的前端性能指标解析
前端·javascript·css
叫我一声阿雷吧2 小时前
JS 入门通关手册(36):变量提升、暂时性死区与块级作用域
javascript·变量提升·暂时性死区·tdz·块级作用域· 前端面试
成都渲染101云渲染66662 小时前
跳出“硬件堆砌”陷阱|渲染101如何用技术重构云渲染的专业价值?
java·前端·javascript
SuperEugene2 小时前
Vue3 性能优化规范:日常必做优化(不玄学、可落地)|可维护性与兜底规范篇
开发语言·前端·javascript·vue.js·性能优化·前端框架
cypking3 小时前
二次封装ElementUI日期范围组件:打造带限制规则的Vue2 v-model响应式通用组件
前端·javascript·elementui
酉鬼女又兒3 小时前
零基础快速入门前端蓝桥杯Web考点深度解析:var、let、const与事件绑定实战(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·es6·html5
happymaker06263 小时前
vue指令扩展以及监视器的使用
前端·javascript·vue.js
还是大剑师兰特3 小时前
EventBus核心方法用法
javascript·vue.js·大剑师