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

跨组件层级传递数据:

如何用react写页面

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-link组件 router-link是Vue Router提供的组件,用于声明式导航: <router…

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

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

实现登录页面跳转的方法 在H5中实现登录页面跳转可以通过多种方式完成,以下是一些常见的方法: 使用JavaScript的window.location方法 通过JavaScript的window.l…

h5实现页面跳转

h5实现页面跳转

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

vue实现 页面

vue实现 页面

Vue 实现页面的基本方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现页面的常见方法: 单文件组件(SFC) 使用 .vue 文件组织页面结构,包含模板、脚…

vue 实现页面返回

vue 实现页面返回

监听浏览器返回事件 使用 window.addEventListener 监听 popstate 事件,在 Vue 的 mounted 钩子中绑定事件,并在 beforeDestroy 钩子中移除监听…

实现js页面跳转

实现js页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现跳转,这是最常用的方法: window.location.href = "https://…