当前位置:首页 > React

如何用react写页面

2026-01-24 19:09:36React

创建React项目

使用create-react-app快速初始化项目,需提前安装Node.js环境。运行命令:

npx create-react-app my-app
cd my-app
npm start

组件化开发

React的核心是组件。函数组件示例:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

类组件示例:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

状态管理

在类组件中使用state

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
  increment = () => {
    this.setState({ count: this.state.count + 1 });
  };
  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>+</button>
      </div>
    );
  }
}

函数组件使用Hooks:

import { useState } from 'react';
function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>+</button>
    </div>
  );
}

生命周期与副作用

类组件生命周期方法:

componentDidMount() {
  console.log('Component mounted');
}
componentDidUpdate() {
  console.log('Component updated');
}
componentWillUnmount() {
  console.log('Component will unmount');
}

函数组件使用useEffect

如何用react写页面

useEffect(() => {
  console.log('Effect runs');
  return () => console.log('Cleanup');
}, [dependency]);

路由配置

安装React Router:

npm install react-router-dom

基本路由示例:

import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
function App() {
  return (
    <Router>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
    </Router>
  );
}

样式处理

内联样式:

<div style={{ color: 'red', fontSize: 20 }}>Text</div>

CSS Modules:

如何用react写页面

import styles from './Button.module.css';
function Button() {
  return <button className={styles.error}>Click</button>;
}

数据获取

使用fetchaxios

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

性能优化

React.memo缓存组件:

const MemoizedComponent = React.memo(MyComponent);

useCallback缓存函数:

const memoizedCallback = useCallback(() => {
  doSomething(a, b);
}, [a, b]);

状态提升

当多个组件需要共享状态时,将状态提升到最近的共同父组件:

function Parent() {
  const [sharedState, setSharedState] = useState(null);
  return (
    <>
      <ChildA state={sharedState} setState={setSharedState} />
      <ChildB state={sharedState} setState={setSharedState} />
    </>
  );
}

上下文API

跨组件层级传递数据:

const ThemeContext = React.createContext('light');
function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Toolbar />
    </ThemeContext.Provider>
  );
}
function Toolbar() {
  return <ThemedButton />;
}
function ThemedButton() {
  const theme = useContext(ThemeContext);
  return <button theme={theme}>Button</button>;
}

标签: 如何用页面
分享给朋友:

相关文章

vue怎么实现页面返回

vue怎么实现页面返回

实现页面返回的方法 在Vue中实现页面返回功能可以通过以下几种方式: 使用Vue Router的go方法 this.$router.go(-1) 该方法接受一个整数参数,表示在历史记录中前进或后退…

vue页面分离的实现

vue页面分离的实现

Vue 页面分离的实现方法 组件化开发 Vue 的核心思想之一是组件化,通过将页面拆分为多个可复用的组件实现分离。每个组件包含独立的模板、逻辑和样式,通过 props 和 events 进行通信。…

h5实现页面跳转

h5实现页面跳转

使用 <a> 标签实现跳转 通过 HTML5 的 <a> 标签实现页面跳转是最基础的方法,适用于静态页面或简单的导航需求。示例代码如下: <a href="target…

如何用react

如何用react

使用React的基本步骤 安装Node.js和npm 确保系统已安装Node.js和npm。Node.js自带npm,可从官网下载安装包。安装完成后,通过命令行验证版本: node -v npm…

uniapp前端页面

uniapp前端页面

Uniapp 前端页面开发指南 Uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端部署(H5、小程序、App等)。以下是开发 Uniapp 前端页面的关键方法和注意事项。…

h5页面实现录音

h5页面实现录音

实现H5页面录音的方法 使用Web Audio API Web Audio API提供音频处理能力,结合getUserMedia可实现录音。核心步骤包括请求麦克风权限、创建音频上下文和处理音频流。…