Mark Ku's Blog

React Learning Notes

Key to the Success of the React Framework

The Problem with Two-Way Binding

Two-way binding primarily changes the UI dynamically by changing the data. As applications grow larger, two-way binding is often unnecessary and instead causes performance overhead, making data increasingly difficult to manage. Angular - AngularJS's approach led to too many derivative problems, which is why Angular 2 and later also adopted a one-way data flow. Vue - A one-way data flow framework. v-model is just syntactic sugar to reduce the code needed to control data flow. It allows user actions to update the bound value through a one-way data flow, and this bound value is then updated on the page through a render update.

One-Way Rendering

Data and the interface are bound together, much like a function: the same input parameters will always produce the same component output.

Virtual DOM

Manipulating the browser's DOM is expensive. JavaScript simulates a DOM abstraction layer to achieve partial render updates with minimal cost.

Diagram illustrating Virtual DOM state change, diff, and re-render
Diagram illustrating Virtual DOM state change, diff, and re-render

Componentization

An application is built from countless components. Components themselves are independent, complete, and freely composable, which enables:

  • Consistency
  • A unified visual style
  • Better collaboration in software development

React and XSS

{html} => When rendered in a JSX template, XSS-related characters are converted into encoded strings. If you need to render raw HTML, you must use dangerouslySetInnerHTML, but be careful when doing so.

import React from "react";
import logo from "./logo.svg";

import Robot from "Robot";
import "App.css";

function App() {
  let html = "<img alt='rebot' src='./images/GDZ.png'} />";
  let jsHack = "javascript: alert('123')";  

  let user = { name: "hannah </script><script>alert(1);//" };

  return (
    <div className="App">      

      <span dangerouslySetInnerHTML={{ __html: user.name }} />
      <a
        className="App-link"
        href="https://reactjs.org"
        target="_blank"
        rel="noopener noreferrer"
      >
        Learn Reactaa
      </a>
      {/* <Robot id="GDZ.png?set=set3" email="edaad" name="mark" /> */}
      <a href={jsHack}>JShACK</a>
      {html}
    </div>
  );
}

export default App;

Methods for Using CSS in React

1. Directly import the entire CSS file

import './index.css'
<div className="app" />

P.S. Importing this way can cause global CSS pollution.

2. Write CSS within the component

import React from "react";

class Header extends React.Component {
	style1 = {
		background: 'red',
		height: '25px'
	};	
	 	
  render() {
    return (
		<div  style={this.style1}>		
			123
		</div>
    );
  }
}

export default Header;

3. CSS-in-JS (Modular import with JSS)

A characteristic of CSS-in-JS is that the resulting CssName in the final render is dynamically generated. The benefit is that you can import and use CSS only when it's needed. Declare the CSS module.

global.d.ts
declare module "*.module.css" {
    const classes: { [key: string]: string}
    export default classes
}

declare module "*.module.scss" {
    const classes: { [key: string]: string}
    export default classes
}
  
import style from './index'
<div className={styles.app}>

VS Code IntelliSense for CSS-in-JS (Install the TypeScript CSS Modules plugin)

npm install typescript-plugin-css-modules --save --dev

Modify the tsconfig.json config file and add plugins

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    ...
  
    "plugins": [
      {
        "name": "typescript-plugin-css-modules"
      }
    ]
    ...
	...
   "include": [
	"global.d.ts"
   ]
   ...
  },
}

Add VS Code configuration (.vscode/settings.json)

{
	"typescript.tsdk": "node_modules/typescript/lib",
	"typescript.enablePromptUseWorkspaceTsdk": true,
	"typescript.tsserver.pluginPaths": ["typescript-plugin-css-modules"]
}

At this point, VS Code will show a prompt. Click "Allow," restart VS Code, and you will have autocompletion for CSS modules.

VS Code prompt in Chinese asking to allow workspace TypeScript version
VS Code prompt in Chinese asking to allow workspace TypeScript version

Autocompletion Feature

VS Code autocompleting CSS module cartContainer property P.S. Looking at React's internals, you'll find that image formats and other types of React modules are already declared by default, so you can just import them directly. VS Code autocompletion for React SVG module declaration

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··492

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··334

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··268

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··218

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··217

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11