当前位置:首页 > 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>;
}

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

相关文章

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://example…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要分为编程式导航和声明式导航两种。以下是具体实现方法: 使用 <router-link> 声明式导航 <r…

h5页面如何实现

h5页面如何实现

实现H5页面的方法 H5页面是基于HTML5技术的网页,通常用于移动端和响应式设计。以下是实现H5页面的关键步骤和技术要点。 基础结构 使用HTML5的DOCTYPE声明作为页面的起始。HTML5简…

jquery页面刷新

jquery页面刷新

jQuery 实现页面刷新 使用 jQuery 刷新页面可以通过以下几种方法实现: 方法一:使用 location.reload() $(document).ready(function() {…

vue 实现反馈页面

vue 实现反馈页面

实现反馈页面的基本结构 使用 Vue.js 创建一个反馈页面需要设计表单组件,包含输入框、下拉选择、评分控件等元素。以下是一个基础模板: <template> <div cla…

vue实现页面注册

vue实现页面注册

使用 Vue 实现用户注册页面 在 Vue 中实现用户注册页面需要结合表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 创建注册表单组件 注册页面通常包含用户名、邮箱、密码等字段: <…