【译】组件与Props

发布时间:2019-08-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【译】组件与Props脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是react官方文档的个人翻译,如有翻译错误,请多多指出
原文地址:https://facebook.github.io/re...

components let you splIT the UI into indePEndent, reusable pieces, and think about each piece in isolation.

组件能让你把UI切割成独立的,可重用的部件,并且能让你独立的思考每一个部件。

Conceptually, components are like JavaScript functions.

从概念上看,components 就像Javascript 的函数。

They accept arbitrary inputs (called "PRops") and return React elements describing what should appear on the screen.

他们都允许任意的输入(叫"props")然后返回React elements ,描述元素应该怎么显示在屏幕。

Functional and Class Components

函数以及类组件

The simplest way to define a component is to write a JavaScript function:

定义组件最简单的方法就是写一个Javascript函数:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

This function is a valid React component because it accepts a single "props" object argument with data and returns a React element.

这个函数是一个合法的React element,因为他接收一个props对象参数并且返回一个React element。

We call such components "functional" because they are literally JavaScript functions.

我们叫这样的组件为函数式组件,因为他们跟Javascipt的函数一样。

You can also use an ES6 class to define a component:

你通常可以使用ES6的class来定义一个组件:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

The above two components are equivalent From React's point of view.

对于React的视图来说,上面这两个组件是等价的。

Classes have some additional features that we will discuss in the next sections.

我们将会在下一章讨论类组件更多的功能。

Until then, we will use functional components for their conciseness.

现在,我们会因为函数式而使用它。

Rendering a Component

渲染组件

Previously, we only encountered React elements that represent DOM tags:
以前,我们只鼓励用React element来表示dom 标签:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Typically, new React apps have a single App component at the very top.

上来讲,一个新的react 应用只有一个App组件在顶部。

However, if you integrate React into an existing app, you might start bottom-up with a small component like Button and gradually work your way to the top of the view hierArchy.

然而,如果你想把react嵌入到现有的应用中,你可能需要从一个小组件像按钮并且逐渐按你的方式替换更高的结构。

Caveat:

Components must return a single root element. This is why we added a <div> to contain all the <Welcome /> elements.

警告:

组件必须返回一个根组件。这就是我们为什么要用一个<div>去包住所有的<Welcome />

Extracting Components

提取组件

Don't be afraid to split components into smaller components.

不要害怕把组件切割成更小的组件

For example, consider this Comment component:
举个例子:思考一下 Comment 组件:

); }" title="" data-original-title="复制">
function Comment(props) {
  return (
    <div classname="Comment">
      <div className="UserInfo">
        <img className="Avatar"
          src={props.author.avatarUrl}
          alt={props.author.name}
        />
        <div className="UserInfo-name">
          {props.author.name}
        </div>
      </div>
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

打开试试

It accepts author (an object), text (a string), and date (a date) as props, and describes a comment on a social media website.

上面的组件接收作者(object), text(字符串),并且日期(date)作为它们的props,并且作为在社交媒体网站上的评论组件。

This component can be tricky to change because of all the nesting, and it is also hard to reuse individual parts of it. Let's extract a few components from it.

因为所有的东西都被嵌套了,所以这个组件要改变就很棘手,并且也很难重用其中的一部分。让我们把这个组件切割成更小的组件。

First, we will extract Avatar:
首先,我们先切割Avatar组件:

function Avatar(props) {
  return (
    <img className="Avatar"
      src={props.user.avatarUrl}
      alt={props.user.name}
    />
  );
}

The Avatar doesn't need to know that it is being rendered inside a Comment.
这个Avatar不需要知道它被Comment组件渲染出来。

This is why we have given its prop a more generic name: user rather than author.
这就是为什么我们给组件prop一个更通用的名字: user 比 author好。

We recommend naming props from the component's own point of view rather than the context in which it is being used.

我们要求命名组件的props的时候,要从组件自身的视图出发,而不是他所被使用的内容上。

We can now simplify Comment a tiny bit:

我们现在可以简化评论组件:

); }" title="" data-original-title="复制">
function Comment(props) {
  return (
    <div className="Comment">
      <div className="UserInfo">
        <Avatar user={props.author} />
        <div className="UserInfo-name">
          {props.author.name}
        </div>
      </div>
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

Next, we will extract a UserInfo component that renders an Avatar next to user's name:

我们将切割成一个渲染Avatar组件 的UserInfo组件,并且包含user名字。

function UserInfo(props) {
  return (
    <div className="UserInfo">
      <Avatar user={props.user} />
      <div className="UserInfo-name">
        {props.user.name}
      </div>
    </div>
  );
}

This lets us simplify Comment even further:

我们更进一步看看这个简化了的组件:

function Comment(props) {
  return (
    <div className="Comment">
      <UserInfo user={props.author} />
      <div className="Comment-text">
        {props.text}
      </div>
      <div className="Comment-date">
        {formatDate(props.date)}
      </div>
    </div>
  );
}

打开试试

Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps.

开始切割组件可能看起来像麻烦的工作,但对于一个大型的应用来说,拥有一个可重用的组件是一个回报很高的事情。

A good rule of thumb is that if a part of your UI is used several times (Button, Panel, Avatar), or is complex enough on its own (App, FeedStory, Comment), it is a good candidate to be a reusable component.

一个好的经验法则是如果你UI中某些组件被重用了多次(如Button, Panel, Avatar),或者对于一个自身就足够复杂的组件(App, FeedStory, Comment)来说,将它们作为可重用的组件是一个好的选择。

Props are Read-Only

Props 是只可读取的

Whether you declare a component as a function or a class, it must never modify its own props. Consider this sum function:

无论你声明一个函数组件或者是一个类组件,它都不能修改他们的props.思考一下下面的相加函数:

function sum(a, b) {
  return a + b;
}

Such functions are called "pure" because they do not attempt to change their inputs, and always return the same result for the same inputs.

像这样的函数,我们称为纯函数,因为他们不要视图改变他们的输入,并且总是返回同样的输出通过输入同样的参数。

In contrast, this function is impure because it changes its own input:

与此形成鲜明对比的是,这个函数是不纯的,因为他改变了自身的参数的值:

function withdraw(account, amount) {
  account.total -= amount;
}

React is pretty flexible but it has a single strict rule:

React是非常灵活的,但是它有一个严格的准则:

All React components must act like pure functions with respect to their props.

所有的React Components 必须要想纯函数那样去接受他们的props

Of course, application UIs are dynamic and change over time.

当然,应用的UI是动态的并且经常变化的。

In the next section, we will introduce a new concept of "state".

在下一章,我们将会介绍一个新的该你那"state"。

State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.

State允许React的组件多次改变它们的输出来响应用户的操作,网络的请求或者别的都不会违背这个准则。

脚本宝典总结

以上是脚本宝典为你收集整理的【译】组件与Props全部内容,希望文章能够帮你解决【译】组件与Props所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:abstractabstractallallbreakbreakbuttonbuttonClassconstconstconstDatedefaultdefaultdivdodocumentDOMelementelementelementselseelsees6extendsflexflexforforformformfunctionfunctionfunctionifininintjavaJSXletnamenamenewnewpost-format-galleryPropReactreturnreturnreturnselectStatetexttextthistoptop数组数组
猜你在找的ES2015(es6)相关文章
其他相关热搜词更多
phpjavapython程序员loadpost-format-gallerydivString参数开发ListcapMapClass工具安装SQL数据库资源this
全站导航更多
最新ES2015(es6)教程
热门ES2015(es6)教程