从Vue快速上手React

news/2024/10/18 7:55:32/

前言

还没使用过React 的 vue同学可以通过这篇博客快速上手React。

1、数据读写

Vue 数据读写:

import { ref, reactive } from 'vue'const str = ref<string>('Aos')
const obj = reactive<Record<string, string>>({name: 'vue',version: '3.2.x'
})
str.value = '66666'
console.log(obj.name,str.value)

React 数据读写:

import React, { useState } from "react";const Component: React.FC = function () {const [getCount, setCount] = useState<number>(0);return (<><div>{getCount}</div><button onClick={() => setCount(getCount + 1)} /></>);
};export default Component;

2、数据监听

Vue提供了 watch 帮忙监听数据变化
React提供了 useEffect 帮忙监听数据变化,但请注意,useEffect还有其他用途,并不局限于此
注意:vue中的数据监听watch可以直接获取新旧值,而react中数据监听useEffect不支持直接获取新旧值

Vue 数据监听:

import { watch, ref, reactive } from 'vue'
const aaa = ref<string>('123456')
const bbb = reactive<Record<string, string>>({name: 'vue',version: '3.3.4'
})
// 单个监听
watch(aaa, (val, oldVal) => {console.log(val)console.log(oldVal)
})
// 多个监听
watch([aaa, bbb], (val, oldVal) => {console.log(val)console.log(oldVal)
}, {deep: true,immediate: true
})

React 数据监听:

import React, { useState, useEffect } from "react";const app = () => {const [count, setCount] = useState<number>(0);const [aaa, setAaa] = useState<number>(100);useEffect(() => {console.log("监听count");// 注意:这个初始化时会执行一次,类似于 watch 的 immediate = true}, [count]);useEffect(() => {console.log("支持多个监听");}, [count, aaa]);return (<><div>count: {count}</div><div>aaa: {aaa}</div><button onClick={() => setCount(count + 1)}>count++</button><buttononClick={() => {setCount(count => count + 1);setAaa(aaa => aaa + 1);}}>多个</button></>);
};export default app;

React 获取新旧值方法示例:

import React, { useState, useEffect, useRef } from "react";export default function RefHookDemo() {const [count, setCount] = useState(0);const countRef = useRef(count);useEffect(() => {console.log("旧值", countRef.current);countRef.current = count;console.log("新值", count);}, [count]);return (<div><h2>前一次的值: {countRef.current}</h2><h2>这一次的值: {count}</h2><button onClick={() => setCount(count + 1)}>+1</button></div>);
}

3、事件绑定

vue 事件绑定:

<!-- vue3 -->
<template><div>{{ count }}<!-- vue dom事件绑定时候必须传 运行函数 --><button @click="setCount()"> +1 </button><button @click="setCount(3)"> +3 </button><button @click="() => { setCount(5) }"> +5 </button><button @click="count += 7"> +7 </button></div>
</template><script lang="ts" setup>
import { ref } from 'vue'const count = ref<number>(0)const setCount =  (addNum: number = 1) => {count.value += addNum
}
</script>

React 事件绑定:

import React, { useState } from "react";const app = () => {const [count, setCount] = useState<number>(0);const addNum = (num: number) => {setCount(count + num);};return (<>{count}{/* react dom事件绑定时候必须传函数类型 */}<button onClick={() => addNum(1)}> +1 </button><button onClick={() => setCount(count + 3)}> +3 </button></>);
};export default app;

4、父子组件通信

父 => 子:

vue和react都一样,通过 props传递

<!-- vue3 -->
<template><childComponent title="data1" :value="23" />
</template>
<script lang="ts" setup>
import childComponent from 'xxxxxx'
</script>
// React
import React from "react";const Child = (porps: any) => {return (<><div>title: {porps.title}</div><div>value: {porps.value}</div></>);
};const Parent = () => {const title = "标签";return <Child title={title} value="23" />;
};export default Parent;

子 => 父:

Vue可以通过emits向父传递回调事件,React可以通过向子组件传递回调函数实现

<!-- vue3 -->
<template><button @click="sendMsg()">向父传消息</button>
</template>
<script lang="ts" setup>
import { defineEmits } from 'vue'
const $emits = defineEmits(['send'])const sendMsg = () => {$emits('send', '我是子组件的回调')
}
</script>
// React
import React, { useCallback } from "react";const Child = (porps: any) => {return <button onClick={porps.cb}>向父传消息</button>;
};const Parent = () => {const getChildEvent = useCallback(() => {console.log("我是子组件的回调");}, []);return <Child cb={getChildEvent} />;
};export default Parent;

使用useCallBack包一下需要传入子组件的那个函数。那样的话,父组件重新渲染,子组件中的函数就会因为被useCallBack保护而返回旧的函数地址,子组件就不会检测成地址变化,也就不会重选渲染。

5、插槽

vue通过插槽方式实现,react通过获取props.children,加载到对应位置实现

Vue 插槽:

<!-- vue3 parent -->
<template><ChildComponent><h1>默认位置</h1><h2 slot="place2">第二位置</h2></ChildComponent>
</template>
<!-- vue3 Child -->
<template><div><!-- 默认位置 --><slot></slot><hr/><!-- 第二位置 --><slot name="place2"></slot></div>
</template>

React 插槽:

import React from "react";const Child = (porps: any) => {return (<>{porps.place}<hr />{porps.place2}</>);
};const Parent = () => {return <Child place={<h1>默认位置</h1>} place2={<h2>第二位置</h2>}></Child>;
};export default Parent;

React 实现 v-if 指令

import { useState } from "react";const Child = (porps: any) => {return <>{porps.show ? <div>条件渲染</div> : null}</>;
};const Parent = () => {const [show, setShow] = useState(false);return (<><button onClick={() => setShow(!show)}>控制条件隐藏</button><Child show={show}></Child></>);
};export default Parent;

React 实现 v-for 指令

import React from "react";const app = () => {const list = [9, 0, 6, 8, 1, 4, 9];return (<>{list.map((item, index) => {return <div key={index}>{item}</div>;})}</>);
};export default app;

React 实现 v-model 指令

import { useState } from "react";const app = () => {const [value, setValue] = useState("");const onChange = e => {setValue(e.target.value);};return (<><input value={value} onChange={onChange} /></>);
};export default app;

http://www.ppmy.cn/news/802412.html

相关文章

汉字拼音首字母

http://hi.baidu.com/stavevai/blog/item/9c76bea574baabff9052ee84.html 为方便拼音用户输入一些汉字&#xff0c;有时候我们需要提供一些拼音首字母的输入方法&#xff0c;这时候需要把相关的汉字的首字母提取出来。 下面这个例子用空间换时间&#xff0c;用查表的方法实现了…

Pinyin4j 汉字转拼音使用教程

目录 Pinyin4j 概述与下载 Pinyin4j 使用快速入门 Pinyin4j 概述与下载 1、pinyin4j 是一个开源的流行 java 库&#xff0c;用来处理中文转换成拼音&#xff0c;拼音输出格式可定制。 官网&#xff1a;http://pinyin4j.sourceforge.net 在线文档&#xff1a;http://pinyin4j…

如何用搜狗拼音输入法输入希腊字母

https://jingyan.baidu.com/article/48b37f8d09d18c1a6464882c.html 方法一&#xff1a;软键盘 右击输入法悬浮窗打开菜单-选择软键盘 这里有很多软键盘&#xff0c;其中第二个就是希腊字母软键盘&#xff0c;点击打开 第二次使用可以点击输入法悬浮窗上的软键盘快捷键来快速…

[转]粤语拼音方案

本文转自&#xff1a;http://inzoi.bokee.com/6981163.html 这里使用的粤语拼音方案的目的是:让所有会普通话拼音与英语的人一看就会,所以排除了引起歧义的写法对于难发的音,就使用两个音相拼的方法,让读音自己连读就可以模仿出近似的音. (如果还是看不懂,最简单的方法就是:下载…

搜狗输入法 VS 拼音加加

用了16年计算机&#xff0c;一共用过四个输入法&#xff1a;五笔&#xff08;牌子忘记了&#xff09;&#xff0c;智能ABC&#xff0c;拼音加加&#xff0c;搜狗。 放弃五笔的原因很简单&#xff1a;我要学拼音&#xff01; 一说到高考&#xff0c;北方人全笑了&#xff0c;语文…

iOS - 找出汉字拼音首字母

#import <Foundation/Foundation.h>interface NSString (PinyinInitials)/**获取汉字拼音的首字母, 返回的字母是大写形式, 例如: "俺妹", 返回 "A".*如果字符串开头不是汉字, 而是字母, 则直接返回该字母, 例如: "b彩票", 返回 "B&q…

拼音对照表

啊a阿a呵a吖a嗄a腌a锕a爱ai矮ai挨ai哎ai碍ai癌ai艾ai唉ai哀ai蔼ai隘ai埃ai皑ai呆ai嗌ai嫒ai瑷ai暧ai捱ai砹ai嗳ai锿ai霭ai按an安an暗an岸an俺an案an鞍an氨an胺an厂an广an庵an揞an犴an铵an桉an谙an鹌an埯an黯an昂ang肮ang盎ang袄ao凹ao傲ao奥ao熬ao懊ao敖ao翱ao澳ao嚣ao拗ao媪ao…

pinyin4j获取汉字正确的全拼和简拼

最近公司开发一个项目&#xff0c;需要获取汉字的拼音然后根据拼音来实现快速搜索。Java方面有PinYin4j开源项目获取汉字的拼音&#xff0c;但是对多音字处理的不好&#xff0c;自己研究了一下&#xff0c;终于给解决了。在这里跟大家分享一下&#xff01; 工程目录结构如下 原…