《React Docs》

  • React is a JavaScript library

  • sparingly/recap/explicitly/synthetic/extract/subsequent/alternative/inclination/hatch

  • After compilation, JSX expressions become regular JavaScript objects.

  • React reads these objects and uses them to construct the DOM and keep it up to date.

  • Elements are the smallest building blocks of React apps.

  • React DOM takes care of updating the DOM to match the React elements.

  • Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

  • React elements are immutable. Once you create an element, you can't change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time.

  • In practice, most React apps only call ReactDOM.render() once.

  • React Only Updates What's Necessary

  • When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object.

  • Always start component names with a capital letter.

  • Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail.

  • Components must return a single root element.

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

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

  • 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.

  • Whether you declare a component as a function or a class, it must never modify its own props.

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

  • State is similar to props, but it is private and fully controlled by the component. (Local state is exactly that: a feature available only to classes)

  • Clock(a component) is now defined as a class rather than a function. This lets us use additional features such as local state and lifecycle hooks.

  • In applications with many components, it's very important to free up resources taken by the components when they are destroyed.

  • The componentDidMount() hook runs after the component output has been rendered to the DOM.

  • If you don't use something in render(), it shouldn't be in the state.

  • There are three things you should know about setState().

    1. Do Not Modify State Directly (The only place where you can assign this.state is the constructor)
    2. State Updates May Be Asynchronous
    3. State Updates are Merged (The merging is shallow)
  • Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.

  • If you imagine a component tree as a waterfall of props, each component's state is like an additional water source that joins it at an arbitrary point but also flows down.

  • Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:

    1. React events are named using camelCase, rather than lowercase.
    2. With JSX you pass a function as the event handler, rather than a string.
  • Another difference is that you cannot return false to prevent default behavior in React. You must call preventDefault explicitly.

  • When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.

  • You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default.

  • Conditional rendering in React works the same way conditions work in JavaScript.

  • You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn't change.

  • Returning null from a component's render method does not affect the firing of the component's lifecycle methods. For instance, componentWillUpdate and componentDidUpdate will still be called.

  • A "key" is a special string attribute you need to include when creating lists of elements.

  • The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.

  • We don't recommend using indexes for keys if the items can reorder, as that would be slow.

  • A good rule of thumb is that elements inside the map() call need keys.

  • Keys Must Only Be Unique Among Siblings. (Keys used within arrays should be unique among their siblings. However they don't need to be globally unique.)

  • Keys serve as a hint to React but they don't get passed to your components.

  • In React, mutable state is typically kept in the state property of components, and only updated with setState()
    .

  • In React, a <textarea> uses a value attribute instead.

  • In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. This is called "lifting state up".

  • We know that props are read-only.

  • There should be a single "source of truth" for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor. Instead of trying to sync the state between different components, you should rely on the top-down data flow.

  • React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
    (继承是一种静态、显性的关系,合成是一种动态、隐形的关系。)

  • Sometimes we think about components as being "special cases" of other components. In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props.

  • At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.

  • Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way.

  • If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.

  • React is, in our opinion, the premier way to build big, fast Web apps with JavaScript.

  • Props are a way of passing data from parent to child.

  • In simpler examples, it's usually easier to go top-down, and on larger projects, it's easier to go bottom-up and write tests as you build.

  • React's one-way data flow (also called one-way binding) keeps everything modular and fast.

  • There are two types of "model" data in React: props and state. It's important to understand the distinction between the two.

  • To build your app correctly, you first need to think of the minimal set of mutable state that your app needs. The key here is DRY: Don't Repeat Yourself.

  • Remember: React is all about one-way data flow down the component hierarchy. It may not be immediately clear which component should own what state.

  • User-Defined Components Must Be Capitalized

  • JSX type can't be an expression.

  • if statements and for loops are not expressions in JavaScript, so they can't be used in JSX directly.

  • Make sure that the expression before && is always boolean.

  • React.PropTypes is deprecated as of React v15.5. Please use the prop-types library instead.

  • With PropTypes.element you can specify that only a single child can be passed to a component as children.

  • In the typical React dataflow, props are the only way that parent components interact with their children.

  • React supports a special attribute that you can attach to any component. The ref attribute takes a callback function, and the callback will be executed immediately after the component is mounted or unmounted.

  • React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts.

  • In the React rendering lifecycle, the value attribute on form elements will override the value in the DOM.

  • We learned earlier that React elements are first-class JS objects and we can store them or pass them around. To render multiple items in React, we pass an array of React elements. The most common way to build that array is to map over your array of data.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容