Skip to content

Commit 75dda5b

Browse files
authored
Add Minimal React Template (#8)
* Add Minimal React Template * Fix Tests
1 parent a34f9c9 commit 75dda5b

25 files changed

+18545
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: web3js-react-dapp-min Build & Test
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
types: [ opened, reopened, synchronize ]
8+
jobs:
9+
build:
10+
name: Build & Test
11+
runs-on: ubuntu-latest
12+
defaults:
13+
run:
14+
working-directory: templates/min/web3js-react-dapp-min
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version: 20
20+
- run: npm i
21+
- run: npm run build
22+
- run: npm test

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ for their selection.
1313
Usage: npx create-web3js-dapp [options]
1414

1515
Options:
16-
-f, --framework <name> front-end framework (choices: "angular")
16+
-f, --framework <name> front-end framework (choices: "angular", "react")
1717
-t, --template <type> template type (choices: "minimal")
1818
-h, --help display help for command
1919
```
@@ -31,3 +31,4 @@ can be used to build dApps.
3131
This utility supports the following front-end frameworks:
3232

3333
- [Angular](https://angular.dev/)
34+
- [React](https://react.dev/)

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ program
1515
.addOption(
1616
new Option("-f, --framework <name>", "front-end framework").choices([
1717
"angular",
18+
"react",
1819
]),
1920
)
2021
.addOption(
@@ -63,6 +64,10 @@ async function inquire(cliOpts: OptionValues): Promise<Options> {
6364
name: "Angular",
6465
value: "angular",
6566
},
67+
{
68+
name: "React",
69+
value: "react",
70+
},
6671
],
6772
});
6873
}

src/lib.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,28 @@ test("getSelectedOption(angular, minimal)", () => {
2929
"unexpected project location",
3030
);
3131
});
32+
33+
test("getSelectedOption(react, minimal)", () => {
34+
const opts: Options = { framework: "react", template: "minimal" };
35+
const result: SelectedOption = getSelectedOption(opts);
36+
37+
const expectedName: string = "web3js-react-dapp-min";
38+
assert.strictEqual(
39+
result.projectName,
40+
expectedName,
41+
"unexpected project name",
42+
);
43+
44+
const expectedLocation: string = join(
45+
__dirname,
46+
"..",
47+
"templates",
48+
"min",
49+
expectedName,
50+
);
51+
assert.strictEqual(
52+
result.projectLocation,
53+
expectedLocation,
54+
"unexpected project location",
55+
);
56+
});

src/lib.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,16 @@ export function getSelectedOption(opts: Options): SelectedOption {
1515
);
1616
return { projectName, projectLocation };
1717
}
18+
case "react": {
19+
const projectName: string = "web3js-react-dapp-min";
20+
const projectLocation: string = join(
21+
__dirname,
22+
"..",
23+
"templates",
24+
"min",
25+
projectName,
26+
);
27+
return { projectName, projectLocation };
28+
}
1829
}
1930
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type Framework = "angular";
1+
export type Framework = "angular" | "react";
22
export type TemplateType = "minimal";
33

44
export interface Options {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 web3js-react-dapp-min
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Web3.js + React Minimal dApp Template
2+
3+
This project is a minimal template for using [Web3.js](https://web3js.org/) with
4+
[React](https://react.dev/).
5+
6+
- [Web3.js Docs](https://docs.web3js.org/)
7+
- [React Docs](https://react.dev/learn)
8+
9+
This project was bootstrapped with
10+
[Create React App](https://github.com/facebook/create-react-app).
11+
12+
## Getting Started
13+
14+
Install the project dependencies with a dependency manager like npm or Yarn. Use
15+
`npm start` to start a local development server and navigate to
16+
http://localhost:3000 to view the dApp.
17+
18+
## Project Design
19+
20+
This project defines a service in
21+
[./src/web3/web3-service.ts](./src/web3/web3-service.ts) that exposes an
22+
instance of the [`Web3` class](https://docs.web3js.org/api/web3/class/Web3) and
23+
an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider, if present. If
24+
the provider is present, it's used by the `Web3` instance to communicate with
25+
the network and the service registers a
26+
[`chainChanged` handler](https://docs.metamask.io/wallet/reference/provider-api/#chainchanged)
27+
that reloads the page. Refer to [./src/App.tsx](./src/App.tsx) for an example of
28+
using the service.

0 commit comments

Comments
 (0)