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

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:

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>;
}

如何用react写页面

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

相关文章

vue实现页面缓存

vue实现页面缓存

使用 <keep-alive> 组件实现缓存 Vue 内置的 <keep-alive> 组件可以缓存动态组件或路由组件。通过包裹需要缓存的组件,可以保留其状态避免重复渲染。…

vue实现点击页面切换

vue实现点击页面切换

Vue 实现页面切换 在 Vue 中实现页面切换通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈。 使用 Vue Router 实现页面切换 Vue Router 是 Vue 官方提供…

vue实现组织架构页面

vue实现组织架构页面

Vue 实现组织架构页面 数据准备 组织架构通常以树形结构展示,需要准备嵌套的节点数据。例如: data() { return { orgData: { id: 1,…

vue如何实现默认页面

vue如何实现默认页面

Vue 实现默认页面的方法 在 Vue 项目中,通常需要设置一个默认页面(如首页或登录页)作为用户访问时的初始页面。可以通过路由配置实现。 配置路由默认跳转 在 Vue Router 中,使用 re…

js实现页面跳转

js实现页面跳转

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

h5实现登录页面跳转页面跳转页面

h5实现登录页面跳转页面跳转页面

实现H5登录页面跳转 在H5中实现登录页面跳转可以通过多种方式完成,以下是几种常见方法: 使用window.location.href window.location.href = '目标页面…