react vue_带Vue命名的插槽来React

news/2024/7/10 2:48:05 标签: vue

react vue

While I was working on a React.js side project using Semantic UI React as a UI library, I noticed that its components are written in a way that reminds me of Vue.js components I write.

当我使用语义UI React作为UI库进行React.js端项目时,我注意到其组件的编写方式使我想起了我编写的Vue.js组件。

If you’ve been writing Vue.js web apps or you took a tour at their documentations, you probably know slots. If not, then let me take you on my tour through this article.

如果您一直在编写Vue.js网络应用程序,或者浏览了他们的文档,那么您可能知道插槽。 如果没有,那么让我带您浏览这篇文章。

什么是Vue插槽? (What are Vue Slots?)

Vue implements a content distribution API inspired by the Web Components spec draft, using the <slot> element to serve as distribution outlets for content.

Vue使用<slot>元素作为内容的分发渠道,实现了受Web组件规范草案启发的内容分发API。

Let’s take an example. If you want to pass a DOM into a child component, we can do the following:

让我们举个例子。 如果要将DOM传递到子组件中,我们可以执行以下操作:

<navigation-link url="/profile">
Your Profile
</navigation-link>

then in the component of navigation-link, you might have

然后在navigation-link组件中,您可能有

<a 
:href="url"
class="nav-link"
>
<slot></slot>
</a>

When the component renders, <slot></slot> will be replaced by “Your Profile”.

渲染组件后, <slot></slot>将被“您的配置文件”替换。

You may wonder and tell yourself where is the difference? It’s doable, and I can access my DOM elements via props.children

您可能想知道并告诉自己区别在哪里? 这是可行的,并且我可以通过props.children访问我的DOM元素

Okay, you’re right you can do this, but there’s a cool feature called named slots that you don’t know about. Let’s find out what it is.

好的,您可以执行此操作,但是您不知道有一个很酷的功能,称为命名插槽 。 让我们找出它是什么。

Vue命名插槽 (Vue Named Slots)

There are times when it’s useful to have multiple slots. For example, in a <default-layout> component with the following structure:

有时候拥有多个插槽很有用。 例如,在具有以下结构的<default-layout>组件中:

We need to pass a piece of code to every slot. For example, the header slot will be replaced with <NavbarComponent /> and so on with the rest of the slots.

我们需要向每个插槽传递一段代码。 例如,标头插槽将由<NavbarComponent />替换,其余的插槽依此类推。

To provide content to these slots to v-slot directive on a <template> like the following snippet:

要将内容提供给<template>上的v-slot指令的这些插槽,如下所示:

Now, everything inside <template> elements will be passed to the corresponding slots. Cool right?

现在, <template>元素内的所有内容都将传递到相应的插槽。 酷吧?

The question that is running on your mind now is How can I do that in React? Let’s take a look at how we could do it in React

您现在想到的问题是我该如何在React中做到这一点? 让我们看看如何在React中做到这一点

Since props.children is an array of elements, we can implement this cool feature in React by using Array.prototype.find like this:

由于props.children是一个元素数组,因此我们可以通过使用Array.prototype.find在React中实现此酷功能,如下所示:

If you use class-based components it’ll be like this:

如果您使用基于类的组件,它将像这样:

So, we’ve just implemented or slots. Let’s use them

因此,我们刚刚实现了或插槽。 让我们使用它们

Now your React components are more readable. Happy Hacking! 🎉🍻

现在,您的React组件更具可读性。 快乐黑客! 🎉🍻

翻译自: https://medium.com/@gazouly/bring-vue-named-slots-to-react-87684188f18e

react vue


http://www.niftyadmin.cn/n/1467986.html

相关文章

react钩子_如何使用简单的react自定义钩子修复内存泄漏

react钩子See error logs in your console with the message “Cannot perform state update on an unmounted component” from your React application? There is a simple cause and easy fix.在您的React应用程序中查看控制台中的错误日志&#xff0c;并显示消息“无法在已…

matlab prctile出错,matlab使用Copula仿真优化市场风险

原标题&#xff1a;matlab使用Copula仿真优化市场风险使用Copula仿真优化市场风险此示例演示了使用具有胖尾边缘分布的多变量copula模拟计算投资组合的风险价值和条件风险值(预期缺口)。然后使用模拟来计算最优风险收益组合的有效前沿。内容导入支持历史数据集可视化标准化价格…

php建立函数库,利用php DOM函数库创建xml文档

/*** filename: domEx.php** Editor: richard_ma** Date: 2007-07-27** Description:* 利用php DOM函数库创建xml文档*/// 设置Http头属性为xmlheader("Content-Type:text/xml");/*** 创建文档对象及根节点*/// 创建DOM对象,xml版本为1.0编码方式为UTF-8$dom new …

最小二乘法解决什么问题_如何解决最小操作问题

最小二乘法解决什么问题Hello, everyone!大家好&#xff01; It’s been quite some time since I last posted, but I am back with another JavaScript algorithm tutorial. This time we’ll be solving the Minimum Operations problem, which tasks you with finding the…

matlab散点图看不到,matlab关于叶绿素散点图无法画出问题

clc;clear all;close all;tablereadtable(F:\Chlorophyll-a\叶绿素csv格式\sw_chla_monthly_2002-2011.csv);%将csv转成表格形式timetable.time(2:362613,:); %获取时间列% xmat2str(cell2mat(time(1,1)));% yx(2:8);%获取月平均的日期dataxlsread(F:\Chlorophyll-a\sw_chla_mo…

迁移学习 保持参数不变_迁移到打字稿,使其保持平稳

迁移学习 保持参数不变In the process of transitioning two mature frontend codebases (totalling ~5k files / 300k lines) from Javascript to Typescript, I learned a bit about the process that might be helpful to anyone considering this change in their stack. M…

ajax调用php文件函数吗,使用php和ajax调用javascript函数?

这是我的情况:我已经加载了这个页面(评论页面)。我可以使用javascript函数(ajax)获取新的注释。我也可以发表评论。问题是,当我发表评论时,我想同时获取新的评论。(所以用户可以看到他上传的评论。)遗憾的是,我的评论获取脚本比我的评论上传脚本快得多,这使得javascript在我的评…

多继承,多重继承,多层继承_服务继承的角度介绍

多继承,多重继承,多层继承Inheritance is one of the most popular ways to reuse code in Angular. It has been used very frequently to build components. Yet, not many developers will apply inheritance to service. In fact, service inheritance is much cleaner and…