@@ -51,9 +51,10 @@ Shopify Theme Lab is a customizable modular development environment for blazing-
51
51
- [ Stylus] ( #stylus )
52
52
- [ Swapping CSS framework] ( swapping-css-framework )
53
53
- [ Removing Tailwind CSS] ( #removing-tailwind-css )
54
- - [ Bulma] ( #Bulma )
54
+ - [ Bulma] ( #bulma )
55
55
- [ Swapping JavaScript framework] ( swapping-javascript-framework )
56
56
- [ Removing Vue] ( #removing-vue )
57
+ - [ React] ( #react )
57
58
- [ Project structure] ( #project-structure )
58
59
- [ Tasks] ( #tasks )
59
60
- [ Development environment concepts] ( #development-environment-concepts )
@@ -92,6 +93,7 @@ Shopify Theme Lab is a customizable modular development environment for blazing-
92
93
- JavaScript
93
94
- [ Vue] ( https://vuejs.org )
94
95
- [ Vuex] ( https://vuex.vuejs.org )
96
+ - Swap Vue with any other framework e.g. [ React] ( https://reactjs.org )
95
97
- [ Axios] ( https://github.com/axios/axios )
96
98
- Extend with [ npm packages] ( https://www.npmjs.com ) 📦
97
99
- CSS
@@ -438,6 +440,110 @@ module: {
438
440
]
439
441
}
440
442
` ` `
443
+
444
+ # ## React
445
+
446
+ 1. Install packages :
447
+
448
+ # ### npm
449
+ ` ` ` sh
450
+ $ npm install react react-dom
451
+ $ npm install @babel/preset-react --save-dev
452
+ ` ` `
453
+
454
+ # ### yarn
455
+ ` ` ` sh
456
+ $ yarn add react react-dom
457
+ $ yarn add @babel/preset-react --dev
458
+ ` ` `
459
+
460
+ 2. Inside [webpack.common.js](.config/webpack/webpack.common.js) :
461
+ ` ` ` js
462
+ ...
463
+ resolve: {
464
+ extensions: [... '.jsx'] // add jsx to extensions array
465
+ }
466
+ ...
467
+ ` ` `
468
+
469
+ ` ` ` js
470
+ ...
471
+ module: {
472
+ rules: [
473
+ // add babel-loader for jsx,
474
+ // remove the same loader from webpack.prod.js
475
+ {
476
+ test: /\. (js|jsx)$/,
477
+ loader: 'babel-loader',
478
+ exclude: /node_modules/,
479
+ options: {
480
+ presets: [ '@babel/preset-env', '@babel/preset-react' ]
481
+ }
482
+ },
483
+ ...
484
+ ]
485
+ }
486
+ ...
487
+ ` ` `
488
+
489
+ 3. Add ecmaFeatures to [.eslint.js](config/.eslint.js) :
490
+ ` ` ` js
491
+ ...
492
+ parserOptions: {
493
+ ecmaVersion: 2020,
494
+ sourceType: 'module',
495
+ ecmaFeatures: { // add ecmaFeatures
496
+ jsx: true,
497
+ modules: true
498
+ }
499
+ }
500
+ ...
501
+ ` ` `
502
+
503
+ 4. Add following code to [main.js](src/main.js) :
504
+ ` ` ` js
505
+ // eslint-disable-next-line
506
+ import React from 'react'
507
+ import { render } from 'react-dom'
508
+
509
+ /**
510
+ * react components
511
+ * auto-map all react components to dom elements
512
+ */
513
+ const reactComponents = require.context('./react/components/', true, /\. (jsx|js)$/)
514
+
515
+ reactComponents.keys().forEach(key => {
516
+ const Component = reactComponents(key).default
517
+
518
+ // transform file name to PascalCase
519
+ const name = key.replace(/\. (\/ |jsx|js)/g, '').replace(/(\/ |-|_|\s )\w /g, (match) => match.slice(1).toUpperCase())
520
+
521
+ const domElement = document.querySelector(` [react-component='${name}']`)
522
+ render(<Component/>, domElement)
523
+ })
524
+ ```
525
+
526
+ 5 . Create ` src/react/components/MyComponent.jsx ` with following content:
527
+ ``` js
528
+ import React from ' react'
529
+
530
+ class MyComponent extends React .Component {
531
+ render () {
532
+ return (
533
+ < div>
534
+ My Component
535
+ < / div>
536
+ )
537
+ }
538
+ }
539
+
540
+ export default MyComponent
541
+ ```
542
+
543
+ 6 . Add a dom element to [ theme.liquid] ( shopify/layout/theme.liquid ) or any other ` .liquid ` file:
544
+ ``` html
545
+ <div react-component =" MyComponent" ></div >
546
+ ```
441
547
<!-- swapping javascript framework (end) -->
442
548
443
549
<!-- project structure (start) -->
0 commit comments