Comments (2)
createElement
:
- 第一个参数是 type 简单来说就是各种 标签名字(包括 HTML 自身的,还有 React 组件名字)
- 第二个参数是传入的属性
- 第三个参数以及之后的参数就是作为组件的子组件
JSX 编写的代码就是转换为这个方法,一般用了 JSX 写法都不会再需要自己直接调用 该方法
cloneElement
- 第一个参数是 一个 React 元素
- 新添加的属性会并入原有的属性
一般配合React.children.map
使用,如用于动态地给子组件添加更多 props 信息、样式
更深一点的原因在于,React 元素是 不可变对象
例如 props.children 获取到的只是一个 描述符,不能直接修改它的任何属性,只能读取他的信息。
所以我们可以选择拷贝它们,然后再修改、添加
from daily-question.
React.cloneElement(
element,
[props],
[...children]
)
React.createElement(
type,
[props],
[...children]
)
直接上 API,很容易得出结论:首参不一样。这也是他们的最大区别:
cloneElement
,根据 Element 生成新的 ElementcreateElement
,根据 Type 生成新的 Element
然而,此时估计还是云里雾里,含糊不清,需要弄清它,首先要明白俩概念
- Type
- Element
React.cloneElement 的使用场景
from daily-question.
Related Issues (20)
- 【Q739】webpack 中是如何处理 new URL 资源的
- 【Q740】vite 中是如何处理 new URL 资源的
- 【Q741】我们上传图片为 Blob/File 对象时,是如何向服务器端传送数据的 HOT 1
- [bug] B站的链接贴错了 HOT 2
- 引用仓库错了 HOT 2
- `<script type="module">` HOT 1
- 多阶段构建并不需要 docker-compose HOT 1
- 【Q742】大文件上传,如何获取到读取进度? HOT 1
- 代码
- [feature request]面试题添加难度排序以及一键生成一份面试题的工具 HOT 2
- 【Q747】如何实现一个 omit/omitBy 函数 HOT 6
- 【Q748】在 babel 编译为低版本 ES 时,为何能够编译可选链之类语法,但无法编译 API HOT 1
- 【Q743】实现 batchFn 函数,可以批量执行函数 HOT 4
- 【开源自荐】推荐一个每日更新的前端面试题库 HOT 1
- 【Q474】在 react 中,以下父子组件的 useEffect/useLayoutEffect 顺序如何 HOT 1
- 【Q745】webpack 的打包流程是什么样的 HOT 1
- 【Q744】数据库中更新一条记录时,如何自动更新其 updated_at 字段
- 【Q746】如何避免 SQL 攻击
- 【Q749】React18 有哪些新特性
- 【Q750】React19 有哪些新特性
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daily-question.