React 组件使用

React 组件是一个 js 函数,函数可以添加 jsx 标记

当前页使用组件,基本使用

注意:组件的名称,第一个字母一定要大写,否则会报错

import { createRoot } from "react-dom/client";
import "./index.css";

const root = createRoot(document.querySelector("#root"));

// 组件的使用
const App = () => {
  const handleIncident = () => {
    console.log("组件的使用");
  };

  return (
    <div>
      <div className="box" onClick={handleIncident}>
        inde.js 内容
      </div>
    </div>
  );
};

// 使用
root.render(<App />);

拆分组件使用

目录结构

image.png

index.js

import { createRoot } from "react-dom/client";

// 引入组件
import App from "./App";

const root = createRoot(document.querySelector("#root"));

// 使用
root.render(<App />);

App.js 现在为出口文件

// 组件的使用
// 引入样式
import "./App.css";
// 引入组件
import Title from "./components/TitleData/index";

// 注意组件的名称,第一个字母一定要大写,否则会报错
const App = () => {
  const handleIncident = () => {
    console.log("组件的使用");
  };

  return (
    <div>
      <div className="box" onClick={handleIncident}>
        <Title></Title>
        inde.js 内容
      </div>
    </div>
  );
};

// 导出
export default App;

组件

components\TitleData\index.js

const Title = () => {
  return <h1>TitleData 标题</h1>;
};

export default Title;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-React组件库是一个同时支持Vue和React的UI组件库,可以在Vue和React项目中使用。下面以Element-React为例,介绍如何使用Vue-React组件库。 1. 安装Element-React:可以使用npm或yarn安装,命令如下: ``` npm install element-react --save 或 yarn add element-react ``` 2. 引入样式文件:在主入口文件中引入Element-React的样式文件,如下: ``` import 'element-react/dist/theme-chalk/index.css'; ``` 3. 引入组件:在Vue或React组件中按需引入需要使用的Element-React组件,如下: 在Vue组件中: ``` <template> <div> <el-button>Vue Button</el-button> </div> </template> <script> import { Button } from 'element-react'; export default { components: { 'el-button': Button } } </script> ``` 在React组件中: ``` import { Button } from 'element-react'; class MyComponent extends React.Component { render() { return ( <div> <Button>React Button</Button> </div> ) } } ``` 4. 使用组件:在Vue或React组件使用引入的Element-React组件,如下: 在Vue组件中: ``` <template> <div> <el-button>Vue Button</el-button> </div> </template> ``` 在React组件中: ``` import { Button } from 'element-react'; class MyComponent extends React.Component { render() { return ( <div> <Button>React Button</Button> </div> ) } } ``` 需要注意的是,不同的Vue-React组件使用方法略有不同,需要根据具体组件库的文档进行使用。同时,也需要根据项目需要选择合适的组件库,避免出现兼容性和功能不匹配的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值