简体   繁体   English

如何在反应中将箭头 Function 转换为 ES5 function

[英]How to convert arrow Function to ES5 function in react

I have this arrow function in react:我有这个箭头 function 反应:

const Welcome = ({subject, description}) => (
<div>
  <Person title={`Welcome ${subject}`}></Person>
  <Profile description={description}></Profile>
</div>
)

and I would like to convert to an es5 function but I am not able to get it to work:我想转换为 es5 function 但我无法让它工作:

var Welcome = function(subject,description) {
   return(
     <Person title="Welcome ".concat(subject)/>
     <Profile description=(description)/>
   );
};

How should I go about it?我应该怎么go一下呢? Thanks谢谢

You need to do following steps:您需要执行以下步骤:

  1. wrap the 2 adjacent JSX elements with a common parent element.用一个共同的父元素包裹 2 个相邻的 JSX 元素。
  2. wrap { } around 'Welcome'.concat(subject) and description .{ }包裹在'Welcome'.concat(subject)description周围。
  3. destructure the function parameter.解构 function 参数。
var Welcome = function({subject, description}) {
   return(
     <div>
        <Person title={ 'Welcome'.concat(subject) }/>
        <Profile description={description}/>
     </div>
   );
};
var Welcome = function(subject,description) {
   return(
     <div>
        <Person title={"Welcome ".concat(subject)}/>
        <Profile description={description}/>
    </div>
   );
};

try this bro试试这个兄弟

You forgot the destructuring in the params and also fogot to wrap the the html content under one parent tag您忘记了参数中的解构,也忘记了将 html 内容包装在一个父标签下

var Welcome = function({subject,description}) {
   return(
     <div>
        <Person title="Welcome ".concat(subject)/>
        <Profile description=(description)/>
    </div>
   );
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM