├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.txt ├── READINGS.md ├── README.md ├── SUMMARY.md ├── anti-patterns ├── 01.props-in-initial-state.md ├── 02.findDOMNode.md ├── 03.mixins.md ├── 04.setState-in-componentWillMount.md ├── 05.mutating-state.md ├── 06.using-indexes-as-key.md ├── 07.spreading-props-dom.md └── README.md ├── book.json ├── coding-style └── README.md ├── docs ├── CONTRIBUTING.md ├── LICENSE.txt ├── READINGS.html ├── anti-patterns │ ├── 01.props-in-initial-state.html │ ├── 02.findDOMNode.html │ ├── 03.mixins.html │ ├── 04.setState-in-componentWillMount.html │ ├── 05.mutating-state.html │ ├── 06.using-indexes-as-key.html │ ├── 07.spreading-props-dom.html │ └── index.html ├── coding-style │ └── README.md ├── gitbook │ ├── fonts │ │ └── fontawesome │ │ │ ├── FontAwesome.otf │ │ │ ├── fontawesome-webfont.eot │ │ │ ├── fontawesome-webfont.svg │ │ │ ├── fontawesome-webfont.ttf │ │ │ ├── fontawesome-webfont.woff │ │ │ └── fontawesome-webfont.woff2 │ ├── gitbook-plugin-edit-link │ │ └── plugin.js │ ├── gitbook-plugin-fontsettings │ │ ├── fontsettings.js │ │ └── website.css │ ├── gitbook-plugin-highlight │ │ ├── ebook.css │ │ └── website.css │ ├── gitbook-plugin-lunr │ │ ├── lunr.min.js │ │ └── search-lunr.js │ ├── gitbook-plugin-search │ │ ├── lunr.min.js │ │ ├── search-engine.js │ │ ├── search.css │ │ └── search.js │ ├── gitbook-plugin-sharing │ │ └── buttons.js │ ├── gitbook.js │ ├── images │ │ ├── apple-touch-icon-precomposed-152.png │ │ └── favicon.ico │ ├── style.css │ └── theme.js ├── gotchas │ ├── 01.pure-render-checks.html │ ├── 02.synthetic-events.html │ └── index.html ├── index.html ├── package.json ├── patterns │ ├── 18.conditionals-in-jsx.html │ ├── 19.async-nature-of-setState.html │ ├── 20.dependency-injection.html │ ├── 21.context-wrapper.html │ ├── 22.event-handlers.html │ ├── 23.flux-pattern.html │ ├── 24.one-way-data-flow.html │ ├── 25.presentational-vs-container.html │ ├── 26.third-party-integration.html │ ├── 27.passing-function-to-setState.html │ ├── 28.decorators.html │ ├── 29.feature-flags-using-redux.html │ ├── 30.component-switch.html │ ├── 31.reaching-into-a-component.html │ ├── 32.list-components.html │ ├── 33.format-text-via-component.html │ └── 34.share-tracking-logic.html ├── perf-tips │ ├── 01.shouldComponentUpdate-check.html │ ├── 02.pure-component.html │ ├── 03.reselect.html │ └── index.html ├── search_index.json ├── styling │ ├── 01.stateless-ui-components.html │ ├── 02.styles-module.html │ ├── 03.style-functions.html │ ├── 04.using-npm-modules.html │ ├── 05.base-component.html │ ├── 06.layout-component.html │ ├── 07.typography-component.html │ ├── 08.HOC-for-styling.html │ └── index.html └── ux-variations │ ├── 01.composing-variations.html │ ├── 02.toggle-ui-elements.html │ ├── 03.HOC-feature-toggles.html │ ├── 04.HOC-props-proxy.html │ ├── 05.wrapper-components.html │ ├── 06.display-order-variations.html │ └── index.html ├── gotchas ├── 01.pure-render-checks.md ├── 02.synthetic-events.md └── README.md ├── package.json ├── patterns ├── 18.conditionals-in-jsx.md ├── 19.async-nature-of-setState.md ├── 20.dependency-injection.md ├── 21.context-wrapper.md ├── 22.event-handlers.md ├── 23.flux-pattern.md ├── 24.one-way-data-flow.md ├── 25.presentational-vs-container.md ├── 26.third-party-integration.md ├── 27.passing-function-to-setState.md ├── 28.decorators.md ├── 29.feature-flags-using-redux.md ├── 30.component-switch.md ├── 31.reaching-into-a-component.md ├── 32.list-components.md ├── 33.format-text-via-component.md └── 34.share-tracking-logic.md ├── perf-tips ├── 01.shouldComponentUpdate-check.md ├── 02.pure-component.md ├── 03.reselect.md └── README.md ├── styling ├── 01.stateless-ui-components.md ├── 02.styles-module.md ├── 03.style-functions.md ├── 04.using-npm-modules.md ├── 05.base-component.md ├── 06.layout-component.md ├── 07.typography-component.md ├── 08.HOC-for-styling.md └── README.md └── ux-variations ├── 01.composing-variations.md ├── 02.toggle-ui-elements.md ├── 03.HOC-feature-toggles.md ├── 04.HOC-props-proxy.md ├── 05.wrapper-components.md ├── 06.display-order-variations.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | _book 2 | node_modules 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | > Your contributions are heartily ♡ welcome. (✿◠‿◠) 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Attribution 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution 4.0 International Public License 58 | 59 | By exercising the Licensed Rights (defined below), You accept and agree 60 | to be bound by the terms and conditions of this Creative Commons 61 | Attribution 4.0 International Public License ("Public License"). To the 62 | extent this Public License may be interpreted as a contract, You are 63 | granted the Licensed Rights in consideration of Your acceptance of 64 | these terms and conditions, and the Licensor grants You such rights in 65 | consideration of benefits the Licensor receives from making the 66 | Licensed Material available under these terms and conditions. 67 | 68 | 69 | Section 1 -- Definitions. 70 | 71 | a. Adapted Material means material subject to Copyright and Similar 72 | Rights that is derived from or based upon the Licensed Material 73 | and in which the Licensed Material is translated, altered, 74 | arranged, transformed, or otherwise modified in a manner requiring 75 | permission under the Copyright and Similar Rights held by the 76 | Licensor. For purposes of this Public License, where the Licensed 77 | Material is a musical work, performance, or sound recording, 78 | Adapted Material is always produced where the Licensed Material is 79 | synched in timed relation with a moving image. 80 | 81 | b. Adapter's License means the license You apply to Your Copyright 82 | and Similar Rights in Your contributions to Adapted Material in 83 | accordance with the terms and conditions of this Public License. 84 | 85 | c. Copyright and Similar Rights means copyright and/or similar rights 86 | closely related to copyright including, without limitation, 87 | performance, broadcast, sound recording, and Sui Generis Database 88 | Rights, without regard to how the rights are labeled or 89 | categorized. For purposes of this Public License, the rights 90 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 91 | Rights. 92 | 93 | d. Effective Technological Measures means those measures that, in the 94 | absence of proper authority, may not be circumvented under laws 95 | fulfilling obligations under Article 11 of the WIPO Copyright 96 | Treaty adopted on December 20, 1996, and/or similar international 97 | agreements. 98 | 99 | e. Exceptions and Limitations means fair use, fair dealing, and/or 100 | any other exception or limitation to Copyright and Similar Rights 101 | that applies to Your use of the Licensed Material. 102 | 103 | f. Licensed Material means the artistic or literary work, database, 104 | or other material to which the Licensor applied this Public 105 | License. 106 | 107 | g. Licensed Rights means the rights granted to You subject to the 108 | terms and conditions of this Public License, which are limited to 109 | all Copyright and Similar Rights that apply to Your use of the 110 | Licensed Material and that the Licensor has authority to license. 111 | 112 | h. Licensor means the individual(s) or entity(ies) granting rights 113 | under this Public License. 114 | 115 | i. Share means to provide material to the public by any means or 116 | process that requires permission under the Licensed Rights, such 117 | as reproduction, public display, public performance, distribution, 118 | dissemination, communication, or importation, and to make material 119 | available to the public including in ways that members of the 120 | public may access the material from a place and at a time 121 | individually chosen by them. 122 | 123 | j. Sui Generis Database Rights means rights other than copyright 124 | resulting from Directive 96/9/EC of the European Parliament and of 125 | the Council of 11 March 1996 on the legal protection of databases, 126 | as amended and/or succeeded, as well as other essentially 127 | equivalent rights anywhere in the world. 128 | 129 | k. You means the individual or entity exercising the Licensed Rights 130 | under this Public License. Your has a corresponding meaning. 131 | 132 | 133 | Section 2 -- Scope. 134 | 135 | a. License grant. 136 | 137 | 1. Subject to the terms and conditions of this Public License, 138 | the Licensor hereby grants You a worldwide, royalty-free, 139 | non-sublicensable, non-exclusive, irrevocable license to 140 | exercise the Licensed Rights in the Licensed Material to: 141 | 142 | a. reproduce and Share the Licensed Material, in whole or 143 | in part; and 144 | 145 | b. produce, reproduce, and Share Adapted Material. 146 | 147 | 2. Exceptions and Limitations. For the avoidance of doubt, where 148 | Exceptions and Limitations apply to Your use, this Public 149 | License does not apply, and You do not need to comply with 150 | its terms and conditions. 151 | 152 | 3. Term. The term of this Public License is specified in Section 153 | 6(a). 154 | 155 | 4. Media and formats; technical modifications allowed. The 156 | Licensor authorizes You to exercise the Licensed Rights in 157 | all media and formats whether now known or hereafter created, 158 | and to make technical modifications necessary to do so. The 159 | Licensor waives and/or agrees not to assert any right or 160 | authority to forbid You from making technical modifications 161 | necessary to exercise the Licensed Rights, including 162 | technical modifications necessary to circumvent Effective 163 | Technological Measures. For purposes of this Public License, 164 | simply making modifications authorized by this Section 2(a) 165 | (4) never produces Adapted Material. 166 | 167 | 5. Downstream recipients. 168 | 169 | a. Offer from the Licensor -- Licensed Material. Every 170 | recipient of the Licensed Material automatically 171 | receives an offer from the Licensor to exercise the 172 | Licensed Rights under the terms and conditions of this 173 | Public License. 174 | 175 | b. No downstream restrictions. You may not offer or impose 176 | any additional or different terms or conditions on, or 177 | apply any Effective Technological Measures to, the 178 | Licensed Material if doing so restricts exercise of the 179 | Licensed Rights by any recipient of the Licensed 180 | Material. 181 | 182 | 6. No endorsement. Nothing in this Public License constitutes or 183 | may be construed as permission to assert or imply that You 184 | are, or that Your use of the Licensed Material is, connected 185 | with, or sponsored, endorsed, or granted official status by, 186 | the Licensor or others designated to receive attribution as 187 | provided in Section 3(a)(1)(A)(i). 188 | 189 | b. Other rights. 190 | 191 | 1. Moral rights, such as the right of integrity, are not 192 | licensed under this Public License, nor are publicity, 193 | privacy, and/or other similar personality rights; however, to 194 | the extent possible, the Licensor waives and/or agrees not to 195 | assert any such rights held by the Licensor to the limited 196 | extent necessary to allow You to exercise the Licensed 197 | Rights, but not otherwise. 198 | 199 | 2. Patent and trademark rights are not licensed under this 200 | Public License. 201 | 202 | 3. To the extent possible, the Licensor waives any right to 203 | collect royalties from You for the exercise of the Licensed 204 | Rights, whether directly or through a collecting society 205 | under any voluntary or waivable statutory or compulsory 206 | licensing scheme. In all other cases the Licensor expressly 207 | reserves any right to collect such royalties. 208 | 209 | 210 | Section 3 -- License Conditions. 211 | 212 | Your exercise of the Licensed Rights is expressly made subject to the 213 | following conditions. 214 | 215 | a. Attribution. 216 | 217 | 1. If You Share the Licensed Material (including in modified 218 | form), You must: 219 | 220 | a. retain the following if it is supplied by the Licensor 221 | with the Licensed Material: 222 | 223 | i. identification of the creator(s) of the Licensed 224 | Material and any others designated to receive 225 | attribution, in any reasonable manner requested by 226 | the Licensor (including by pseudonym if 227 | designated); 228 | 229 | ii. a copyright notice; 230 | 231 | iii. a notice that refers to this Public License; 232 | 233 | iv. a notice that refers to the disclaimer of 234 | warranties; 235 | 236 | v. a URI or hyperlink to the Licensed Material to the 237 | extent reasonably practicable; 238 | 239 | b. indicate if You modified the Licensed Material and 240 | retain an indication of any previous modifications; and 241 | 242 | c. indicate the Licensed Material is licensed under this 243 | Public License, and include the text of, or the URI or 244 | hyperlink to, this Public License. 245 | 246 | 2. You may satisfy the conditions in Section 3(a)(1) in any 247 | reasonable manner based on the medium, means, and context in 248 | which You Share the Licensed Material. For example, it may be 249 | reasonable to satisfy the conditions by providing a URI or 250 | hyperlink to a resource that includes the required 251 | information. 252 | 253 | 3. If requested by the Licensor, You must remove any of the 254 | information required by Section 3(a)(1)(A) to the extent 255 | reasonably practicable. 256 | 257 | 4. If You Share Adapted Material You produce, the Adapter's 258 | License You apply must not prevent recipients of the Adapted 259 | Material from complying with this Public License. 260 | 261 | 262 | Section 4 -- Sui Generis Database Rights. 263 | 264 | Where the Licensed Rights include Sui Generis Database Rights that 265 | apply to Your use of the Licensed Material: 266 | 267 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 268 | to extract, reuse, reproduce, and Share all or a substantial 269 | portion of the contents of the database; 270 | 271 | b. if You include all or a substantial portion of the database 272 | contents in a database in which You have Sui Generis Database 273 | Rights, then the database in which You have Sui Generis Database 274 | Rights (but not its individual contents) is Adapted Material; and 275 | 276 | c. You must comply with the conditions in Section 3(a) if You Share 277 | all or a substantial portion of the contents of the database. 278 | 279 | For the avoidance of doubt, this Section 4 supplements and does not 280 | replace Your obligations under this Public License where the Licensed 281 | Rights include other Copyright and Similar Rights. 282 | 283 | 284 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 285 | 286 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 287 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 288 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 289 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 290 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 291 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 292 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 293 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 294 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 295 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 296 | 297 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 298 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 299 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 300 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 301 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 302 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 303 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 304 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 305 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 306 | 307 | c. The disclaimer of warranties and limitation of liability provided 308 | above shall be interpreted in a manner that, to the extent 309 | possible, most closely approximates an absolute disclaimer and 310 | waiver of all liability. 311 | 312 | 313 | Section 6 -- Term and Termination. 314 | 315 | a. This Public License applies for the term of the Copyright and 316 | Similar Rights licensed here. However, if You fail to comply with 317 | this Public License, then Your rights under this Public License 318 | terminate automatically. 319 | 320 | b. Where Your right to use the Licensed Material has terminated under 321 | Section 6(a), it reinstates: 322 | 323 | 1. automatically as of the date the violation is cured, provided 324 | it is cured within 30 days of Your discovery of the 325 | violation; or 326 | 327 | 2. upon express reinstatement by the Licensor. 328 | 329 | For the avoidance of doubt, this Section 6(b) does not affect any 330 | right the Licensor may have to seek remedies for Your violations 331 | of this Public License. 332 | 333 | c. For the avoidance of doubt, the Licensor may also offer the 334 | Licensed Material under separate terms or conditions or stop 335 | distributing the Licensed Material at any time; however, doing so 336 | will not terminate this Public License. 337 | 338 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 339 | License. 340 | 341 | 342 | Section 7 -- Other Terms and Conditions. 343 | 344 | a. The Licensor shall not be bound by any additional or different 345 | terms or conditions communicated by You unless expressly agreed. 346 | 347 | b. Any arrangements, understandings, or agreements regarding the 348 | Licensed Material not stated herein are separate from and 349 | independent of the terms and conditions of this Public License. 350 | 351 | 352 | Section 8 -- Interpretation. 353 | 354 | a. For the avoidance of doubt, this Public License does not, and 355 | shall not be interpreted to, reduce, limit, restrict, or impose 356 | conditions on any use of the Licensed Material that could lawfully 357 | be made without permission under this Public License. 358 | 359 | b. To the extent possible, if any provision of this Public License is 360 | deemed unenforceable, it shall be automatically reformed to the 361 | minimum extent necessary to make it enforceable. If the provision 362 | cannot be reformed, it shall be severed from this Public License 363 | without affecting the enforceability of the remaining terms and 364 | conditions. 365 | 366 | c. No term or condition of this Public License will be waived and no 367 | failure to comply consented to unless expressly agreed to by the 368 | Licensor. 369 | 370 | d. Nothing in this Public License constitutes or may be interpreted 371 | as a limitation upon, or waiver of, any privileges and immunities 372 | that apply to the Licensor or You, including from the legal 373 | processes of any jurisdiction or authority. 374 | 375 | 376 | ======================================================================= 377 | 378 | Creative Commons is not a party to its public licenses. 379 | Notwithstanding, Creative Commons may elect to apply one of its public 380 | licenses to material it publishes and in those instances will be 381 | considered the "Licensor." Except for the limited purpose of indicating 382 | that material is shared under a Creative Commons public license or as 383 | otherwise permitted by the Creative Commons policies published at 384 | creativecommons.org/policies, Creative Commons does not authorize the 385 | use of the trademark "Creative Commons" or any other trademark or logo 386 | of Creative Commons without its prior written consent including, 387 | without limitation, in connection with any unauthorized modifications 388 | to any of its public licenses or any other arrangements, 389 | understandings, or agreements concerning use of licensed material. For 390 | the avoidance of doubt, this paragraph does not form part of the public 391 | licenses. 392 | 393 | Creative Commons may be contacted at creativecommons.org. 394 | -------------------------------------------------------------------------------- /READINGS.md: -------------------------------------------------------------------------------- 1 | # Related Links 2 | - [React in Patterns by krasimir](https://github.com/krasimir/react-in-patterns) 3 | - [React Patterns by planningcenter](https://github.com/planningcenter/react-patterns) 4 | - [reactpatterns.com](https://github.com/chantastic/reactpatterns.com) 5 | - [10 React Mini-patterns](https://hackernoon.com/10-react-mini-patterns-c1da92f068c5) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [React Bits](https://vasanthk.gitbooks.io/react-bits) 2 | 3 | 有关React,你需要知道的一切 4 | 5 | **Gitbook format**: https://hateonion.github.io/react-bits-CN/ 6 | 7 | 原Repo地址 https://github.com/vasanthk/react-bits 8 | 9 | > Your contributions are heartily ♡ welcome. (✿◠‿◠) 10 | > 新司机上路, 欢迎大家随时提issue和pr进行指正 11 | 12 | - Design Patterns and Techniques 13 | - ✔ ️[Conditional in JSX](./patterns/18.conditionals-in-jsx.md) 14 | - ✔ [Async Nature Of setState()](./patterns/19.async-nature-of-setState.md) 15 | - ✔ [Dependency Injection](./patterns/20.dependency-injection.md) 16 | - ✔ [Context Wrapper](./patterns/21.context-wrapper.md) 17 | - ✔ [Event Handlers](./patterns/22.event-handlers.md) 18 | - ✔ [Flux Pattern](./patterns/23.flux-pattern.md) 19 | - ✔ [One Way Data Flow](./patterns/24.one-way-data-flow.md) 20 | - ✔ [Presentational vs Container](./patterns/25.presentational-vs-container.md) 21 | - ✔ [Third Party Integration](./patterns/26.third-party-integration.md) 22 | - ✔ [Passing Function To setState()](./patterns/27.passing-function-to-setState.md) 23 | - ✔ [Decorators](./patterns/28.decorators.md) 24 | - ✔ [Feature Flags](./patterns/29.feature-flags-using-redux.md) 25 | - ✔ [Component Switch](./patterns/30.component-switch.md) 26 | - ✔ [Reaching Into A Component](./patterns/31.reaching-into-a-component.md) 27 | - ✔ [List Components](./patterns/32.list-components.md) 28 | - ✔ [Format Text via Component](./patterns/33.format-text-via-component.md) 29 | - ✔ [Share Tracking Logic](./patterns/34.share-tracking-logic.md) 30 | - Anti-Patterns 31 | - ✔ [Introduction](./anti-patterns/README.md) 32 | - ✔ [Props In Initial State](./anti-patterns/01.props-in-initial-state.md) 33 | - ✔ [findDOMNode()](./anti-patterns/02.findDOMNode.md) 34 | - ✔ [Mixins](./anti-patterns/03.mixins.md) 35 | - ✔ [setState() in componentWillMount()](./anti-patterns/04.setState-in-componentWillMount.md) 36 | - ✔ [Mutating State](./anti-patterns/05.mutating-state.md) 37 | - ✔ [Using Indexes as Key](./anti-patterns/06.using-indexes-as-key.md) 38 | - ✔ [Spreading Props on DOM elements](./anti-patterns/07.spreading-props-dom.md) 39 | - Handling UX Variations 40 | - 👍 [Introduction](./ux-variations/README.md) 41 | - ✔ [Composing UX Variations](./ux-variations/01.composing-variations.md) 42 | - ✔ [Toggle UI Elements](./ux-variations/02.toggle-ui-elements.md) 43 | - ✔ [HOC for Feature Toggles](./ux-variations/03.HOC-feature-toggles.md) 44 | - ✔ [HOC props proxy](./ux-variations/04.HOC-props-proxy.md) 45 | - ✔ [Wrapper Components](./ux-variations/05.wrapper-components.md) 46 | - ✔ [Display Order Variations](./ux-variations/06.display-order-variations.md) 47 | - Perf Tips 48 | - ✔ [Introduction](./perf-tips/README.md) 49 | - ✔ [shouldComponentUpdate() check](./perf-tips/01.shouldComponentUpdate-check.md) 50 | - ✔ [Using Pure Components](./perf-tips/02.pure-component.md) 51 | - ✔ [Using reselect](./perf-tips/03.reselect.md) 52 | - Styling 53 | - ✔ [Introduction](./styling/README.md) 54 | - ✔ [Stateless UI Components](./styling/01.stateless-ui-components.md) 55 | - ✔ [Styles Module](./styling/02.styles-module.md) 56 | - ✔ [Style Functions](./styling/03.style-functions.md) 57 | - ✔ [NPM Modules](./styling/04.using-npm-modules.md) 58 | - ✔ [Base Component](./styling/05.base-component.md) 59 | - ✔ [Layout Component](./styling/06.layout-component.md) 60 | - ✔ [Typography Component](./styling/07.typography-component.md) 61 | - ✔ [HOC for Styling](./styling/08.HOC-for-styling.md) 62 | - Gotchas 63 | - ✔ [Introduction](./gotchas/README.md) 64 | - ✔ [Pure render checks](./gotchas/01.pure-render-checks.md) 65 | - ✔ [Synthetic Events](./gotchas/02.synthetic-events.md) 66 | - [Related Links](./READINGS.md) 67 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | * [Introduction](README.md) 4 | * Design Patterns and Techniques 5 | * [Conditional in JSX](./patterns/18.conditionals-in-jsx.md) 6 | * [Async Nature Of setState()](./patterns/19.async-nature-of-setState.md) 7 | * [Dependency Injection](./patterns/20.dependency-injection.md) 8 | * [Context Wrapper](./patterns/21.context-wrapper.md) 9 | * [Event Handlers](./patterns/22.event-handlers.md) 10 | * [Flux Pattern](./patterns/23.flux-pattern.md) 11 | * [One Way Data Flow](./patterns/24.one-way-data-flow.md) 12 | * [Presentational vs Container](./patterns/25.presentational-vs-container.md) 13 | * [Third Party Integration](./patterns/26.third-party-integration.md) 14 | * [Passing Function To setState()](./patterns/27.passing-function-to-setState.md) 15 | * [Decorators](./patterns/28.decorators.md) 16 | * [Feature Flags](./patterns/29.feature-flags-using-redux.md) 17 | * [Component Switch](./patterns/30.component-switch.md) 18 | * [Reaching Into A Component](./patterns/31.reaching-into-a-component.md) 19 | * [List Components](./patterns/32.list-components.md) 20 | * [Format Text via Component](./patterns/33.format-text-via-component.md) 21 | * [Share Tracking Logic](./patterns/34.share-tracking-logic.md) 22 | * Anti-Patterns 23 | * [Introduction](./anti-patterns/README.md) 24 | * [Props In Initial State](./anti-patterns/01.props-in-initial-state.md) 25 | * [findDOMNode()](./anti-patterns/02.findDOMNode.md) 26 | * [Mixins](./anti-patterns/03.mixins.md) 27 | * [setState() in componentWillMount()](./anti-patterns/04.setState-in-componentWillMount.md) 28 | * [Mutating State](./anti-patterns/05.mutating-state.md) 29 | * [Using Indexes as Key](./anti-patterns/06.using-indexes-as-key.md) 30 | * [Spreading Props on DOM elements](./anti-patterns/07.spreading-props-dom.md) 31 | * Handling UX Variations 32 | * [Introduction](./ux-variations/README.md) 33 | * [Composing UX Variations](./ux-variations/01.composing-variations.md) 34 | * [Toggle UI Elements](./ux-variations/02.toggle-ui-elements.md) 35 | * [HOC for Feature Toggles](./ux-variations/03.HOC-feature-toggles.md) 36 | * [HOC props proxy](./ux-variations/04.HOC-props-proxy.md) 37 | * [Wrapper Components](./ux-variations/05.wrapper-components.md) 38 | * [Display Order Variations](./ux-variations/06.display-order-variations.md) 39 | * Perf Tips 40 | * [Introduction](./perf-tips/README.md) 41 | * [shouldComponentUpdate() check](./perf-tips/01.shouldComponentUpdate-check.md) 42 | * [Using Pure Components](./perf-tips/02.pure-component.md) 43 | * [Using reselect](./perf-tips/03.reselect.md) 44 | * Styling 45 | * [Introduction](./styling/README.md) 46 | * [Stateless UI Components](./styling/01.stateless-ui-components.md) 47 | * [Styles Module](./styling/02.styles-module.md) 48 | * [Style Functions](./styling/03.style-functions.md) 49 | * [npm Modules](./styling/04.using-npm-modules.md) 50 | * [Base Component](./styling/05.base-component.md) 51 | * [Layout Component](./styling/06.layout-component.md) 52 | * [Typography Component](./styling/07.typography-component.md) 53 | * [HOC for Styling](./styling/08.HOC-for-styling.md) 54 | * Gotchas 55 | * [Introduction](./gotchas/README.md) 56 | * [Pure render checks](./gotchas/01.pure-render-checks.md) 57 | * [Synthetic Events](./gotchas/02.synthetic-events.md) 58 | * [Related Links](./READINGS.md) 59 | 60 | -------------------------------------------------------------------------------- /anti-patterns/01.props-in-initial-state.md: -------------------------------------------------------------------------------- 1 | # 根据props去初始化state 2 | 3 | 前言: 4 | > 使用props去在getInitialState中生成初始state(或者在constructor中初始化)很容易导致多个数据源的问题, 也会给使用者带来这样的疑问: 我们的真正的数据源到底来自哪? 5 | > 这是因为getInitialState只在组件第一次初始化的时候被调用一次. 6 | 7 | 这样做的危险在于, 有可能组件的props发生了改变但是组件却没有被更新.(见下面的例子) 8 | 新的props的值不会被React认为是更新的数据因为构造器(constructor)或者getInitialState方法在组件创建之后不会再次被调用了,因此组件的state不再会被更新. 9 | 要记住, State的初始化只会在组件第一次初始化的时候发生. 10 | 11 | #### 坏的实践 12 | ```javascript 13 | class SampleComponent extends Component { 14 | // constructor function (or getInitialState) 15 | constructor(props) { 16 | super(props); 17 | this.state = { 18 | flag: false, 19 | inputVal: props.inputValue 20 | }; 21 | } 22 | 23 | render() { 24 | return
').html(content); 65 | 66 | $link.appendTo($title); 67 | $title.appendTo($li); 68 | $content.appendTo($li); 69 | $li.appendTo($searchList); 70 | }); 71 | } 72 | 73 | function launchSearch(q) { 74 | // Add class for loading 75 | $body.addClass('with-search'); 76 | $body.addClass('search-loading'); 77 | 78 | // Launch search query 79 | throttle(gitbook.search.query(q, 0, MAX_RESULTS) 80 | .then(function(results) { 81 | displayResults(results); 82 | }) 83 | .always(function() { 84 | $body.removeClass('search-loading'); 85 | }), 1000); 86 | } 87 | 88 | function closeSearch() { 89 | $body.removeClass('with-search'); 90 | $bookSearchResults.removeClass('open'); 91 | } 92 | 93 | function launchSearchFromQueryString() { 94 | var q = getParameterByName('q'); 95 | if (q && q.length > 0) { 96 | // Update search input 97 | $searchInput.val(q); 98 | 99 | // Launch search 100 | launchSearch(q); 101 | } 102 | } 103 | 104 | function bindSearch() { 105 | // Bind DOM 106 | $searchInput = $('#book-search-input input'); 107 | $bookSearchResults = $('#book-search-results'); 108 | $searchList = $bookSearchResults.find('.search-results-list'); 109 | $searchTitle = $bookSearchResults.find('.search-results-title'); 110 | $searchResultsCount = $searchTitle.find('.search-results-count'); 111 | $searchQuery = $searchTitle.find('.search-query'); 112 | 113 | // Launch query based on input content 114 | function handleUpdate() { 115 | var q = $searchInput.val(); 116 | 117 | if (q.length == 0) { 118 | closeSearch(); 119 | } 120 | else { 121 | launchSearch(q); 122 | } 123 | } 124 | 125 | // Detect true content change in search input 126 | // Workaround for IE < 9 127 | var propertyChangeUnbound = false; 128 | $searchInput.on('propertychange', function(e) { 129 | if (e.originalEvent.propertyName == 'value') { 130 | handleUpdate(); 131 | } 132 | }); 133 | 134 | // HTML5 (IE9 & others) 135 | $searchInput.on('input', function(e) { 136 | // Unbind propertychange event for IE9+ 137 | if (!propertyChangeUnbound) { 138 | $(this).unbind('propertychange'); 139 | propertyChangeUnbound = true; 140 | } 141 | 142 | handleUpdate(); 143 | }); 144 | 145 | // Push to history on blur 146 | $searchInput.on('blur', function(e) { 147 | // Update history state 148 | if (usePushState) { 149 | var uri = updateQueryString('q', $(this).val()); 150 | history.pushState({ path: uri }, null, uri); 151 | } 152 | }); 153 | } 154 | 155 | gitbook.events.on('page.change', function() { 156 | bindSearch(); 157 | closeSearch(); 158 | 159 | // Launch search based on query parameter 160 | if (gitbook.search.isInitialized()) { 161 | launchSearchFromQueryString(); 162 | } 163 | }); 164 | 165 | gitbook.events.on('search.ready', function() { 166 | bindSearch(); 167 | 168 | // Launch search from query param at start 169 | launchSearchFromQueryString(); 170 | }); 171 | 172 | function getParameterByName(name) { 173 | var url = window.location.href; 174 | name = name.replace(/[\[\]]/g, '\\$&'); 175 | var regex = new RegExp('[?&]' + name + '(=([^]*)|&|#|$)', 'i'), 176 | results = regex.exec(url); 177 | if (!results) return null; 178 | if (!results[2]) return ''; 179 | return decodeURIComponent(results[2].replace(/\+/g, ' ')); 180 | } 181 | 182 | function updateQueryString(key, value) { 183 | value = encodeURIComponent(value); 184 | 185 | var url = window.location.href; 186 | var re = new RegExp('([?&])' + key + '=.*?(&|#|$)(.*)', 'gi'), 187 | hash; 188 | 189 | if (re.test(url)) { 190 | if (typeof value !== 'undefined' && value !== null) 191 | return url.replace(re, '$1' + key + '=' + value + '$2$3'); 192 | else { 193 | hash = url.split('#'); 194 | url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, ''); 195 | if (typeof hash[1] !== 'undefined' && hash[1] !== null) 196 | url += '#' + hash[1]; 197 | return url; 198 | } 199 | } 200 | else { 201 | if (typeof value !== 'undefined' && value !== null) { 202 | var separator = url.indexOf('?') !== -1 ? '&' : '?'; 203 | hash = url.split('#'); 204 | url = hash[0] + separator + key + '=' + value; 205 | if (typeof hash[1] !== 'undefined' && hash[1] !== null) 206 | url += '#' + hash[1]; 207 | return url; 208 | } 209 | else 210 | return url; 211 | } 212 | } 213 | }); 214 | -------------------------------------------------------------------------------- /docs/gitbook/gitbook-plugin-sharing/buttons.js: -------------------------------------------------------------------------------- 1 | require(['gitbook', 'jquery'], function(gitbook, $) { 2 | var SITES = { 3 | 'facebook': { 4 | 'label': 'Facebook', 5 | 'icon': 'fa fa-facebook', 6 | 'onClick': function(e) { 7 | e.preventDefault(); 8 | window.open('http://www.facebook.com/sharer/sharer.php?s=100&p[url]='+encodeURIComponent(location.href)); 9 | } 10 | }, 11 | 'twitter': { 12 | 'label': 'Twitter', 13 | 'icon': 'fa fa-twitter', 14 | 'onClick': function(e) { 15 | e.preventDefault(); 16 | window.open('http://twitter.com/home?status='+encodeURIComponent(document.title+' '+location.href)); 17 | } 18 | }, 19 | 'google': { 20 | 'label': 'Google+', 21 | 'icon': 'fa fa-google-plus', 22 | 'onClick': function(e) { 23 | e.preventDefault(); 24 | window.open('https://plus.google.com/share?url='+encodeURIComponent(location.href)); 25 | } 26 | }, 27 | 'weibo': { 28 | 'label': 'Weibo', 29 | 'icon': 'fa fa-weibo', 30 | 'onClick': function(e) { 31 | e.preventDefault(); 32 | window.open('http://service.weibo.com/share/share.php?content=utf-8&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)); 33 | } 34 | }, 35 | 'instapaper': { 36 | 'label': 'Instapaper', 37 | 'icon': 'fa fa-instapaper', 38 | 'onClick': function(e) { 39 | e.preventDefault(); 40 | window.open('http://www.instapaper.com/text?u='+encodeURIComponent(location.href)); 41 | } 42 | }, 43 | 'vk': { 44 | 'label': 'VK', 45 | 'icon': 'fa fa-vk', 46 | 'onClick': function(e) { 47 | e.preventDefault(); 48 | window.open('http://vkontakte.ru/share.php?url='+encodeURIComponent(location.href)); 49 | } 50 | } 51 | }; 52 | 53 | 54 | 55 | gitbook.events.bind('start', function(e, config) { 56 | var opts = config.sharing; 57 | 58 | // Create dropdown menu 59 | var menu = $.map(opts.all, function(id) { 60 | var site = SITES[id]; 61 | 62 | return { 63 | text: site.label, 64 | onClick: site.onClick 65 | }; 66 | }); 67 | 68 | // Create main button with dropdown 69 | if (menu.length > 0) { 70 | gitbook.toolbar.createButton({ 71 | icon: 'fa fa-share-alt', 72 | label: 'Share', 73 | position: 'right', 74 | dropdown: [menu] 75 | }); 76 | } 77 | 78 | // Direct actions to share 79 | $.each(SITES, function(sideId, site) { 80 | if (!opts[sideId]) return; 81 | 82 | gitbook.toolbar.createButton({ 83 | icon: site.icon, 84 | label: site.text, 85 | position: 'right', 86 | onClick: site.onClick 87 | }); 88 | }); 89 | }); 90 | }); 91 | -------------------------------------------------------------------------------- /docs/gitbook/images/apple-touch-icon-precomposed-152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hateonion/react-bits-CN/72aea1e7d280612341ba101bf82c0173816ca180/docs/gitbook/images/apple-touch-icon-precomposed-152.png -------------------------------------------------------------------------------- /docs/gitbook/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hateonion/react-bits-CN/72aea1e7d280612341ba101bf82c0173816ca180/docs/gitbook/images/favicon.ico -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-bits", 3 | "version": "1.0.0", 4 | "repository": "https://github.com/vasanthk/react-bits.git", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "docs": "gitbook serve" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /gotchas/01.pure-render-checks.md: -------------------------------------------------------------------------------- 1 | # 保证渲染的性能 2 | 为了保障组件的性能, 我们有的时候会从组件渲染的角度出发. 3 | 4 | 更干净的render函数? 这个概念可能会有点让人疑惑. 5 | 6 | 其实在这里干净是指我们在`shouldComponentUpdate`这个生命周期函数里面去做浅比较, 从而避免不必要的渲染. 7 | 8 | 关于上面的干净渲染, 现有的一些实现包括React.PureComponent, PureRenderMixin, recompose/pure 等等. 9 | 10 | #### 第一个例子 11 | 12 | ##### 坏实践 13 | ```javascript 14 | class Table extends PureComponent { 15 | render() { 16 | return ( 17 |
True!
:True!
14 | }; 15 | ``` 16 | 如果像下面一样有很多的三元运算符的话: 17 | 18 | ```javascript 19 | // Y soo many ternary??? :-/ 20 | const sampleComponent = () => { 21 | return ( 22 |Blah
26 | : flag5 27 | ?Meh
28 | :Herp
29 | :Derp
30 | } 31 |Blah
51 | } else if (flag5) { 52 | returnMeh
53 | } else { 54 | returnHerp
55 | } 56 | } else { 57 | returnDerp
58 | } 59 | })() 60 | } 61 |Derp
; 72 | if (flag4) returnBlah
; 73 | if (flag5) returnMeh
; 74 | returnHerp
75 | } 76 | ``` 77 | 78 | ### 参考资料: 79 | - https://engineering.musefind.com/our-best-practices-for-writing-react-components-dec3eb5c3fc8 80 | - [Conditional rendering](https://facebook.github.io/react/docs/conditional-rendering.html) 81 | 82 | 83 | -------------------------------------------------------------------------------- /patterns/19.async-nature-of-setState.md: -------------------------------------------------------------------------------- 1 | # setState函数的异步性 2 | 3 | ## 简述 4 | 5 | 在某些情况下,React框架出于性能优化考虑,可能会将多次state更新合并成一次更新。正因为如此,setState实际上是一个异步的函数。 6 | 但是,有一些行为也会阻止React框架本身对于多次state更新的合并,从而让state的更新变得同步化。 7 | 比如: eventListeners, Ajax, setTimeout 等等。 8 | 9 | ### 详解 10 | 11 | 当setState() 函数执行的时候,函数会创建一个暂态的state作为过渡state,而不是立即修改this.state。 12 | 如果在调用setState()函数之后尝试去访问this.state,你得到的可能还是setState()函数执行之前的结果。 13 | 在使用setState()的情况下,看起来同步执行的代码其实执行顺序是得不到保证的。原因上面也提到过,React可能会将多次state更新合并成一次更新来优化性能。 14 | 15 | 运行下面这段代码,你会发现当和addEventListener, setTimeout 函数或者发出AJAX call的时候,调用setState, state会发生改变。并且render函数会在setState()函数被触发之后马上被调用。那么到底发生了什么呢?事实上,类似setTimeout()函数或者发出ajax call的fetch函数属于调用浏览器层面的API,这些函数的执行并不存在与React的上下文中,所以React并不能够像控制其他存在与其上下文中的函数一样,将多次state更新合并成一次。 16 | 17 | 在上面这些例子中,React框架之所以在选择在调用setState函数之后立即更新state而不是采用框架默认的方式,即合并多次state更新为一次更新,是因为这些函数调用(fetch,setTimeout等浏览器层面的API调用)并不处于React框架的上下文中,React没有办法对其进行控制。React在此时采用的策略就是及时更新,确保在这些函数执行之后的其他代码能拿到正确的数据(即更新过的state)。 18 | 19 | ```javascript 20 | class TestComponent extends React.Component { 21 | 22 | constructor(props) { 23 | super(props); 24 | this.state = { 25 | dollars: 10 26 | } 27 | } 28 | 29 | componentDidMount() { 30 | // Add custom event via `addEventListener` 31 | // 32 | // The list of supported React events does include `mouseleave` 33 | // via `onMouseLeave` prop 34 | // 35 | // However, we are not adding the event the `React way` - this will have 36 | // effects on how state mutates 37 | // 38 | // Check the list here - https://facebook.github.io/react/docs/events.html 39 | document.getElementById('testButton').addEventListener('mouseleave', this.onMouseLeaveHandler); 40 | 41 | // Add JS timeout 42 | // 43 | // Again,outside React `world` - this will also have effects on how state 44 | // mutates 45 | setTimeout(this.onTimeoutHandler, 10000); 46 | 47 | // Make AJAX request 48 | fetch('https://api.github.com/users') 49 | .then(this.onAjaxCallback); 50 | } 51 | 52 | onClickHandler = () => { 53 | console.log('State before (_onClickHandler): ' + JSON.stringify(this.state)); 54 | this.setState({ 55 | dollars: this.state.dollars + 10 56 | }); 57 | console.log('State after (_onClickHandler): ' + JSON.stringify(this.state)); 58 | } 59 | 60 | onMouseLeaveHandler = () => { 61 | console.log('State before (mouseleave): ' + JSON.stringify(this.state)); 62 | this.setState({ 63 | dollars: this.state.dollars + 20 64 | }); 65 | console.log('State after (mouseleave): ' + JSON.stringify(this.state)); 66 | } 67 | 68 | onTimeoutHandler = () => { 69 | console.log('State before (timeout): ' + JSON.stringify(this.state)); 70 | this.setState({ 71 | dollars: this.state.dollars + 30 72 | }); 73 | console.log('State after (timeout): ' + JSON.stringify(this.state)); 74 | } 75 | 76 | onAjaxCallback = (err, res) => { 77 | if (err) { 78 | console.log('Error in AJAX call: ' + JSON.stringify(err)); 79 | return; 80 | } 81 | 82 | console.log('State before (AJAX call): ' + JSON.stringify(this.state)); 83 | this.setState({ 84 | dollars: this.state.dollars + 40 85 | }); 86 | console.log('State after (AJAX call): ' + JSON.stringify(this.state)); 87 | } 88 | 89 | render() { 90 | console.log('State in render: ' + JSON.stringify(this.state)); 91 | 92 | return ( 93 | 98 | ); 99 | } 100 | } 101 | 102 | ReactDOM.render( 103 |Add new tag:
44 |One lamb is
One jet is
Those gumboots will set ya back
43 |
One lamb is {numberToPrice(lambPrice)}
76 |One jet is {numberToPrice(jetPrice, { showDecimals: false })}
77 |Those gumboots will set ya back 78 | {numberToPrice(bootPrice, { showDecimals: false, showSymbol: false })} 79 | bucks.
80 |