简体   繁体   English

ReactJS中使用组件代替单个变量有什么用

[英]What is the use of components in ReactJS instead of a single variable

I started to study ReactJS and can't understand the purpose of components.开始研究ReactJS,看不懂组件的用途。 for instance lets have a component:例如让我们有一个组件:

var QuoteMaker = React.createClass({
 render: function () {
return (
  <blockquote>
    <p>
      The world is full of objects, more or less interesting; I do not wish to add any more.
    </p>
    <cite>
      <a target="_blank"
        href="#">
        Douglas Huebler
      </a>
    </cite>
  </blockquote>
);}});

why can't we just use为什么我们不能使用

var thisVar = (
<blockquote>
<p>
  The world is full of objects, more or less interesting; I do not wish to    add any more.
  </p>
 <cite>
  <a target="_blank"
    href="#">
    Douglas Huebler
  </a>
</cite>
</blockquote>
)

instead of that component.而不是那个组件。

React allow you to get a composant approach. React 允许您获得组合方法。 So you can use as a new balise text (composant) in react.因此,您可以在 react 中用作新的 balise 文本(组合)。 You can reuse this component, it will have the same behavior everywhere.你可以重用这个组件,它在任何地方都会有相同的行为。

You could.你可以。 That would be just fine.那就好了。

What you couldn't do is have a list of those, built from dynamic data.你不能做的是有一个由动态数据构建的列表。 What if you wanted multiple quotes from multiple people?如果您想要多个人的多个引用怎么办?

What if you wanted a button that randomized which quote appeared?如果你想要一个随机出现哪个引用的按钮怎么办?

What if you wanted a twitter feed, or a blog roll, or a comment section?如果您想要 Twitter 提要、博客滚动或评论部分怎么办?

You don't necessarily need the createClass .您不一定需要createClass That's a particularly old way of looking at things, and you should really be using a Babel build pipeline;这是一种特别古老的看待事物的方式,您确实应该使用 Babel 构建管道; full stop.句号。

You can simply say:你可以简单地说:

const Quote = (props) => (
  <blockquote>
    <p>{ props.quote }</p>
    <cite >{ props.author }</cite>
  </blockquote>
);

ReactDOM.render(<Quote quote="..." author="..." />, rootEl);

Now you can happily build a randomizer, or a quote API, or a quote editor, or whatever.现在,您可以愉快地构建随机生成器、报价 API、报价编辑器或其他任何东西。 The component does what it should, and it leans on input from the outside world for what it shouldn't do.该组件做它应该做的事情,它依靠外部世界的输入来做它不应该做的事情。

  1. Functional components功能组件

That way that you have in example can be created function, or stateless components, where we can use props object, that can be passed from the parent object.这样,您在示例中可以创建函数或无状态组件,我们可以在其中使用可以从父对象传递的props对象。

const Component = (props) => {
    //access to props
}
  1. Class component类组件

Class components allows to store thier own state and use it to rerender the component, via this.setState() function:类组件允许存储他们自己的状态并使用它来重新渲染组件,通过this.setState()函数:

class Component extends React.Component {
   constructor(props) {
      super(props);
      this.state = {};
   }
   //access to both state and props
}

Why we must use components instead of js objects?为什么我们必须使用组件而不是 js 对象? Because react has its own DOM - virtual DOM , where it listening to changes and rerendering your real DOM, you can reuse or extend this component anywhere and its also semanticly awesome.因为 React 有它自己的 DOM - virtual DOM ,它监听变化并重新渲染你的真实 DOM,你可以在任何地方重用或扩展这个组件,它的语义也很棒。

Take a look to thedocumentation about React components.查看有关 React 组件的文档

There are lots of uses for components.组件有很多用途。 These are few:这些是少数:

  1. Allegedly, if your component has no logics or special use, just flat html, so can pass using component, although it is more encapsulated and looks more neat.据称,如果你的组件没有逻辑或特殊用途,只是扁平化的html,那么可以通过使用组件,虽然它更封装,看起来更整洁。
  2. If now or in the future you'll want to use some logics for your component, so it can be handled in the component.如果现在或将来您希望为组件使用一些逻辑,以便可以在组件中处理它。 That is the Object Oriented approach for using html block of code.这就是使用 html 代码块的面向对象方法。 If your'e coming from Angular, you can think of it as "directive", from .Net "User control".如果您来自 Angular,您可以将其视为来自 .Net“用户控制”的“指令”。 Well, not exactly, but the purpose is same.嗯,不完全是,但目的是一样的。
  3. Calls and uses to different libraries.调用和使用不同的库。 It is a better practice not to require libraries as global variable, but only for your use in the component.更好的做法是不要求库作为全局变量,而只供您在组件中使用。
  4. And of course the most important factor: You can take advantage of the react "digest loop".当然最重要的因素是:您可以利用反应“摘要循环”。 You can choose what to do on constructing, before rendering, after rendering, and much more.您可以选择在构建时、渲染前、渲染后等执行的操作。 You can look it up here: https://facebook.github.io/react/docs/react-component.html你可以在这里查看: https : //facebook.github.io/react/docs/react-component.html

As i said before, there are lots of uses for components over flat html, and basically this is the whole point of ReactJS as component based :)正如我之前所说,组件在平面 html 上有很多用途,基本上这就是 ReactJS 作为基于组件的重点:)

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

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