react框架如何使用
安装 React
使用 Create React App 快速搭建项目:
npx create-react-app my-app
cd my-app
npm start
组件基础
函数组件示例:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
类组件示例:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
状态管理
函数组件使用 Hook:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
类组件状态管理:
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
生命周期
类组件生命周期方法:

class Clock extends React.Component {
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
time: new Date().toLocaleTimeString()
});
}
render() {
return (
<div>
<h1>Current Time:</h1>
<h2>{this.state.time}</h2>
</div>
);
}
}
函数组件使用 useEffect:
import { useState, useEffect } from 'react';
function Clock() {
const [time, setTime] = useState(new Date().toLocaleTimeString());
useEffect(() => {
const timerID = setInterval(() => {
setTime(new Date().toLocaleTimeString());
}, 1000);
return () => clearInterval(timerID);
}, []);
return (
<div>
<h1>Current Time:</h1>
<h2>{time}</h2>
</div>
);
}
事件处理
事件绑定示例:
function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
}
return (
<a href="#" onClick={handleClick}>
Click me
</a>
);
}
列表渲染
使用 map() 渲染列表:

function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
<li key={number.toString()}>
{number}
</li>
);
return <ul>{listItems}</ul>;
}
表单处理
受控组件示例:
function NameForm() {
const [value, setValue] = useState('');
function handleChange(event) {
setValue(event.target.value);
}
function handleSubmit(event) {
alert('A name was submitted: ' + value);
event.preventDefault();
}
return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input type="text" value={value} onChange={handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
组件通信
父传子:
function Parent() {
const message = "Hello from parent";
return <Child message={message} />;
}
function Child(props) {
return <h1>{props.message}</h1>;
}
子传父:
function Parent() {
function handleChildClick(data) {
console.log("Data from child:", data);
}
return <Child onClick={handleChildClick} />;
}
function Child(props) {
function handleClick() {
props.onClick("Data from child");
}
return <button onClick={handleClick}>Click me</button>;
}
使用 Context
创建和使用 Context:
const ThemeContext = React.createContext('light');
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
return (
<div>
<ThemedButton />
</div>
);
}
function ThemedButton() {
const theme = useContext(ThemeContext);
return <button theme={theme}>I am styled by theme!</button>;
}






