Next Steps Last but not least, you not only want to share your code, but you may also want to distribute your package. The most popular package manager for JavaScript is npm, which we've used through the book to install packages and dependencies. In the next section, we will see how easy is it to publish a new package with npm. Other than npm, some developers may need to add your component as a global dependency and use it without a package manager. As we saw in the first chapter, you can easily use React by just adding a script tag pointing to unpkg.com. It is important to give the users of your library the same option. So, to offer a global version of your package, you should build the Universal Module Definition (UMD) version as well. With Webpack this is pretty straightforward: You just have to set the libraryTarget in the output section of the configuration file. Publishing a npm package The most popular way of making a package available to developers is publishing it to npm, the package manager for Node.js. We used it in all the examples in this book and you have seen how easy it is to install a package: It is just a matter of running npm install package, and that is it. What you may not know is how easy it is to publish a package as well. First of all, if you move into an empty directory and write in your terminal: npm init A new package.json is created and some questions will be displayed. The first one is the package name, which defaults to the folder name, and then the version number. These are the most important ones because the first is the name that the users of your package will refer to when they install and use it; the second helps you release new versions of your package safely and without breaking other people's code. The version number is composed of three numbers separated by a dot and they all have a meaning. The last number of the package on the right represents the patch, and it should be increased when a new version of the library that contains bug fixes is pushed to npm. [ 288 ]
Next Steps The number in the middle indicates the minor version of the release and it should be changed when new features are added to the library and those new features do not break existing APIs. Finally, the first number on the left represent the major version and it has to be increased when a version containing breaking changes is released to the public. Following this approach, called Semantic Versioning (SemVer), is good practice and it makes your users more confident when they have to update your package. The first version of a package is usually the 0.1.0. To publish an npm package you must have an npm account, which you can easily create by running the following command in the console: npm adduser $username Where $username is the name of your choice. Once the user is created you can run: npm publish A new entry will be added to the registry with the package name and the version you specified in the package.json. Whenever you change something in your library and you want to push a new version, you just have to run: npm version $type Where $type is one patch, minor or major. This command will bump the version automatically in your package.json and it will also create a commit and a tag if your folder is under version control. Once the version number is increased you just have to run npm publish again and the new version will be available to users. [ 289 ]
Next Steps Summary In the last stop in this trip around the React world, we have seen some of the aspects that make React great: its community and its ecosystem, and how to contribute to them. You learned how to open an Issue if you find a bug in React, and the steps to take to make it easier for its core developers to fix it. You now know the best practices when making code open source and the benefits and the responsibilities that come with it. Finally, you saw how easy it is to publish packages on the npm registry and how to choose the right version number to avoid breaking other people's code. [ 290 ]
A Index animations sub-rendering 37, 38 about 143 components creating 144, 145 React motion 145 about 11, 13 communication between 82 Atomic CSS Modules 172, 173 composition 170 Container and Presentational pattern 85, 86, 87, B 88, 90 Babel Contributor License Agreement (CLA) about 51 installing 21, 22 about 283 plugins 225 reference 283 controlled components big picture about 134 reference 284 JSON schema 134 Controlled form component 262 Browserify 163 core-notes reference 26 C CSS Modules about 162 callbacks 109 Atomic CSS Modules 172 camelCased 26 locally scoped CSS 166, 170, 171 children 11, 83, 84, 85 project, setting up 163, 164, 165 classes, creating React CSS Modules 173, 174 Webpack 162 about 51 CSS createClass factory, using 51 animations 156 main differences 52 in JS 151 React.Component, extending 52 reference 163 stateless functional components 57 currying process 47 code coverage tools 255, 257 code D distributing 285, 286, 287, 288 codebase 90 data flow common misconceptions 14, 16 about 107, 108 common patterns, JSX child-parent communication (callbacks) 109 conditionals 30, 31, 33, 34 common parent 110, 111 control statements 35, 36 loops 35 data multi-line 29, 30 fetching 111, 112, 114, 115, 117 fetching, example 191, 192, 193
dead code elimination 152 about 45 declarative programming 8, 9 composition 48 differences, classes currying 47, 48 first-class objects 45 autobinding 55, 57 FP 48, 49 props, defining 52, 53, 54 immutability 47 state 54 purity 46 Document Object Model 200 user interfaces 48, 49 DOM elements props, spreading 279, 280, 281 G DOM node 11 domProps 281 gists Don't Repeat Yourself (DRY) 74, 265 reference 112 E GitHub reference 17 Edge cases 230 element 10 global keyword 170 Enzyme 228 good first bug error handling reference 284 React, using 267, 268, 269 ESLint H about 38, 205 Higher-order Components (HoCs) 94, 95, 96 Airbnb configuration 44, 45 about 46, 158 configuration 39, 40, 41 testing 258, 259, 260, 261 installation 39 React plugin 42, 43, 44 Higher-order Functions (HoFs) 45, 94 event bubbling 139 event delegation 139 I event switch reference 138 immutability 47, 223 events 136, 137, 138 immutable data 224 Express 180, 184 index F using, as key 276, 279 inline styles fetch API 192 fetch about 151 using 153, 154, 156 reference 112 isomorphic application 180 forms J about 126 controlled components 131, 132, 133 JavaScript Fatigue 14, 194 JSON schema 135, 136 JavaScript testing uncontrolled components 126, 127, 128, 129, Jest, using 230, 231, 233, 234, 236, 237 130 utilities, for React 242, 243, 244 Jest Function as Child 103, 104 about 228 Functional CSS 172 used, for performing painless JavaScript testing functional programming (FP) 230 JSFiddle [ 292 ]
reference 283 O JSX, and HTML differences oneOf utility 69 about 25 optimization techniques attribute 25 Boolean attributes 27 about 205 root 26 components, updating 206, 207, 208 spaces 26, 27 stateless functional components 208 styles 25 Owner 82 JSX about 20, 21 P and HTML, differences 25 Babel, installing 21 Page Object pattern 265 children 24 Perf Add-on 279 common patterns 29 polyfill 112 DOM elements 23 presentation 85 Hello, World! 22, 23 prop types JavaScript templating 28 props 23 about 68 React components 23 React Docgen 70 spread attributes 28 props about 82 L spreading, on DOM elements 279, 280, 281 used, for initializing state 270, 271, 272, 273 libraries 223 Pull Request 284 linters 37 locally scoped class names 151 R logic 85 Radium M working 158, 159, 161 Media queries 156 React constant elements transformer 226 mixins 91, 92 React Dev Tools 266, 267 MobX 61 React Docgen 50 Mocha 228, 238, 239, 240, 241, 242 React element 10 mock 236 React inline elements transform 226 module bundlers 163 React Storybook 287 monitoring tools 224 React tree motion component 146 Mustache 12 Snapshot Testing 253, 254, 255 react-motion library 125 N react-refetch library 117, 118, 119, 120, 122 React Next.js 179, 194, 195, 197 npm package about 266 contributing 283 publishing 288, 289 JavaScript testing utilities 242 reference 15 used, for error handling 267 using 11, 13, 14 real-world testing example 245, 246, 248, 251, 252 [ 293 ]
realworld example, Redux about 61 reference 31 asynchronous 62, 63 derivables 65 recompose library external libraries 61 about 98 initializing, props used 270, 271, 272, 273 context 101 mutating 273, 274, 276 React lumberjack 64 reconciliation 11, 200, 201, 202, 204 render method 66, 67 Redux 61 using 64, 65 Refs working 61, 62 stateless function 50 about 139, 140, 141, 143 stateless functional components React motion 146 about 57, 58 render method 200 context 58 ResponsiveMixin 94 event handlers 59 reusable components 72, 73, 74, 76, 82 lifecycle 59 no reference to component 60 S optimization 60 props 58 Scalable Vector Graphics (SVG) 147 refs 59 Search Engine Optimization (SEO) 181 state 59 Semantic Versioning (SemVer) 289 this keyword 59 server-side application Storybook 87 Style Guide 87 creating 184, 185, 189, 190 style guide Server-Side Rendering (SSR) about 76 checking 79 about 180 creating 76, 77, 78, 80 better performance 183 styled components 175 code base 182 subtree 208 complexity 183, 184 Synthetic Event 136 implementing 181 Search Engine Optimization (SEO) 181 T Shallow rendering 228 ShallowWrapper 243 Tagged Template Literals 175 single event handler 138 Test Driven Development (TDD) 230 single global handler 139 testing solutions Snapshot Testing 228, 253 solutions, re-rendering issues about 257 about 209 Higher-Order Components 258 components, no updation requires 209 Page Object pattern 262, 264, 265, 266 constant props 215 testing functions, creating inside render method 212, benefits 229, 230 TestUtils 228 214 Theming 177 TodoMVC example 245 functions, creating render method 211 TodoTextInput good design 217, 219, 221, 223 refactoring 217, 219, 220, 222 spread attributes reference 28 spring function 146 state [ 294 ]
reference 245 universal applications 180, 181 tools 223 Universal Module Definition (UMD) 288 U W Uncontrolled Components 125 webkit 154 Unidirectional Data Flow 107 Webpack 163, 205, 288
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308