简体   繁体   English

我们如何在 React JavaScript 中使用管道?

[英]How we can use pipes in React JavaScript?

In Angular we can create a custom pipe to change data format.在 Angular 中,我们可以创建一个自定义管道来更改数据格式。 For example, see the UpperCasePipe :例如,请参阅UpperCasePipe

{{ value_expression | uppercase }}

There doesn't appear to be pipes in React, so my question is: How can we use pipes in React? React 中似乎没有管道,所以我的问题是:我们如何在 React 中使用管道?

There are simply no pipes, because anything evaluated in the brackets is considered plain JavaScript.根本就没有管道,因为在括号中计算的任何内容都被认为是普通的 JavaScript。 Because this is evaluated as plain javascript, there is simply no need for pipes.因为这被评估为普通的 javascript,所以根本不需要管道。 If this.state.variable is a string, I can use the .toUpperCase() prototype to display the string in all uppercase:如果this.state.variable是一个字符串,我可以使用.toUpperCase()原型以全大写显示字符串:

{this.state.variable.toUpperCase()}

This would be functionally equivalent to UpperCasePipe :这在功能上等同于UpperCasePipe

{{variable | uppercase}}

It's easy enough to just create a wrapper function, and use that.只需创建一个包装函数并使用它就很容易了。 For example, if you wanted to create a custom function to uppercase just the first letter, we can use this function from here :例如,如果您想创建一个只大写第一个字母的自定义函数,我们可以从这里使用此函数:

function capitalizeFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

We then would call it like so:然后我们会这样称呼它:

{capitalizeFirstLetter(this.state.variable)}

Expressions within {} in JSX are just plain JavaScript. JSX 中{}中的表达式只是普通的 JavaScript。 | is bitwise OR .是按位OR There are no Angular-like "pipes".没有类似 Angular 的“管道”。 A JavaScript function achieves the same thing, though.不过,JavaScript 函数可以实现相同的功能。

Alternatively, to get memoisation, try the new useMemo() hook或者,要获得记忆,请尝试新的useMemo()钩子

In the simplest case, you just need to call a function in the "template" (although there are no templates in React: it's just JSX syntax, a sugar on top of existing standard, in order to allow you to write XML-like code for calling a function in the middle of regular JavaScript file).在最简单的情况下,你只需要在“模板”中调用一个函数(尽管 React 中没有模板:它只是 JSX 语法,在现有标准之上的一个糖,以便允许你编写类似 XML 的代码用于在常规 JavaScript 文件中间调用函数)。

So what would in Angular be a pipe with name set to 'uppercase' , can in React be a simple function:那么在 Angular 中什么是name设置为'uppercase'的管道,在 React 中可以是一个简单的函数:

function uppercase (value: string) { return /* ... */ }  

In React, you'd just call this function in the middle of the "template" with no care in the world:在 React 中,你只需在“模板”中间调用这个函数,而无需关心:

<div> { uppercase(value_expression) } </div>

But there's something that they don't tell you: Angular pipes are by default marked as pure functions and Angular doesn't re-run the pipe code unless its inputs change.但是有些事情他们没有告诉你:默认情况下,Angular 管道被标记为函数,除非其输入发生变化,否则 Angular 不会重新运行管道代码。 With the React "equivalent" shown above, you don't get that.使用上面显示的 React “等效项”,您不会明白这一点。 Instead, every time a component is re-rendered (which can be quite often, as often as typing or clicking anything in the app), the whole body of the function will be re-evaluated.取而代之的是,每次重新渲染组件时(这可能很频繁,就像在应用程序中键入或单击任何内容一样频繁),整个函数体都将被重新评估。

Now, for such a simple case as an uppercase pipe/function, that's not a big deal, but you might have some more complex calculations, or might have the same calculation repeated numerous times (such as in a large data table).现在,对于uppercase管道/函数这样简单的情况,这没什么大不了的,但您可能有一些更复杂的计算,或者可能多次重复相同的计算(例如在大型数据表中)。 In such cases, this is not an acceptable behavior.在这种情况下,这是不可接受的行为。

Therefore, you'd need a caching or memoization strategy for your React functions which act as pipes in Angular.因此,您需要为在 Angular 中充当管道的 React 函数使用缓存或记忆策略 And, guess what, just like everything else in React, the library doesn't solve it for you, so you have to hunt down such a simple thing yourself, decide on which you want to use, install the package, read its docs, and use it.而且,你猜怎么着,就像 React 中的其他所有东西一样,库不会为你解决它,所以你必须自己寻找这样一个简单的东西,决定你想使用哪个,安装包,阅读它的文档,并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在 Javascript React Native 中使用 import 或 require 中的变量吗 - Can we use variable in import or require in Javascript React Native 我们如何使用 Javascript 和 asp 验证控件一起使用 - How we can use Use Javascript and asp validation control together 我们如何使用危险的 SetInnerHTML 和 read-more-react - npm? - How can we use dangerouslySetInnerHTML with read-more-react - npm? 我们如何使用react-intl中未列出的语言? - How can we use a language which is not listed in react-intl? 我们如何在 react-native 中使用多线程 - How can we use multithreading in react-native 我们如何使用javascript标记一段文本? - How can we use javascript to tokenise a piece of text? 我们如何在JavaScript中将三元运算符用于多个条件? - How can we use the ternary operator with multiple condition in JavaScript? 我们可以在 react js 中调用 javascript function 吗? - Can we call javascript function in react js? React-Native:我们可以直接使用 JavaScript 的 'debounce' function 还是 _lodash 的 debounce 是唯一的选择? - React-Native: Can we use JavaScript's 'debounce' function directly or _lodash's debounce is the only option? 我们也可以对容器使用React Context吗? - Can we use React Context for containers as well?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM