Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 1ba83ba

Browse files
committed
fix publish issue, improve readme
1 parent a4a6afb commit 1ba83ba

File tree

4 files changed

+68
-147
lines changed

4 files changed

+68
-147
lines changed

.prettierrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const config = {
55
importOrderSeparation: true,
66
importOrderSortSpecifiers: true,
77
bracketSpacing: false,
8-
printWidth: 120,
8+
printWidth: 100,
99
semi: true,
1010
singleQuote: false,
1111
trailingComma: "all",

README.md

Lines changed: 46 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,77 @@
1-
# TSDX React User Guide
1+
# 🔀 React use bireducer [![tests](https://img.shields.io/github/workflow/status/soywod/react-use-bireducer/integration?label=tests&logo=github&style=flat-square)](https://github.com/soywod/react-use-bireducer/actions/workflows/test.yaml) [![codecov](https://img.shields.io/codecov/c/github/soywod/react-use-bireducer?logo=codecov&style=flat-square)](https://app.codecov.io/gh/soywod/react-use-bireducer) [![npm](https://img.shields.io/npm/v/react-use-bireducer?logo=npm&label=npm&color=success&style=flat-square)](https://www.npmjs.com/package/react-use-bireducer)
22

3-
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
3+
React hook for managing effects within reducers.
44

5-
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
6-
7-
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
8-
9-
## Commands
10-
11-
TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
12-
13-
The recommended workflow is to run TSDX in one terminal:
5+
## Installation
146

157
```bash
16-
npm start # or yarn start
8+
yarn add react-use-bireducer
9+
# or
10+
npm install react-use-bireducer
1711
```
1812

19-
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
20-
21-
Then run the example inside another:
22-
23-
```bash
24-
cd example
25-
npm i # or yarn to install dependencies
26-
npm start # or yarn start
27-
```
28-
29-
The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, we use [Parcel's aliasing](https://parceljs.org/module_resolution.html#aliases).
30-
31-
To do a one-off build, use `npm run build` or `yarn build`.
32-
33-
To run tests, use `npm test` or `yarn test`.
13+
## Usage
3414

35-
## Configuration
15+
The API is very close to `useReducer`, except that:
3616

37-
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
17+
- The state reducer returns `[State, Effect[]]` instead of `State`
18+
- You need to pass an effect reducer
3819

39-
### Jest
20+
```typescript
21+
import {useBireducer} from "react-use-bireducer";
4022

41-
Jest tests are set up to run with `npm test` or `yarn test`.
23+
declare function stateReducer(state: State, action: Action): [State, Effect[]];
24+
declare function effectReducer(effect: Effect): EffectCleanup | void;
4225

43-
### Bundle analysis
44-
45-
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
46-
47-
#### Setup Files
48-
49-
This is the folder structure we set up for you:
50-
51-
```txt
52-
/example
53-
index.html
54-
index.tsx # test your component here in a demo app
55-
package.json
56-
tsconfig.json
57-
/src
58-
index.tsx # EDIT THIS
59-
/test
60-
blah.test.tsx # EDIT THIS
61-
.gitignore
62-
package.json
63-
README.md # EDIT THIS
64-
tsconfig.json
26+
const [state, dispatch] = useBireducer(stateReducer, effectReducer, defaultState);
6527
```
6628

67-
#### React Testing Library
68-
69-
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
70-
71-
### Rollup
72-
73-
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
74-
75-
### TypeScript
76-
77-
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
78-
79-
## Continuous Integration
29+
See a complete [live example on
30+
CodeSandbox](https://codesandbox.io/s/react-use-bireducer-example-20n30w?file=/src/App.tsx).
8031

81-
### GitHub Actions
32+
## Development
8233

83-
Two actions are added by default:
34+
Development environment is managed by [Nix](https://nixos.org/). First
35+
you need to install it:
8436

85-
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
86-
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
87-
88-
## Optimizations
89-
90-
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
91-
92-
```js
93-
// ./types/index.d.ts
94-
declare var __DEV__: boolean;
95-
96-
// inside your code...
97-
if (__DEV__) {
98-
console.log('foo');
99-
}
37+
```bash
38+
curl -L https://nixos.org/nix/install | sh
10039
```
10140

102-
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
103-
104-
## Module Formats
105-
106-
CJS, ESModules, and UMD module formats are supported.
107-
108-
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
109-
110-
## Deploying the Example Playground
111-
112-
The Playground is just a simple [Parcel](https://parceljs.org) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`):
41+
Then you can start your development environment by spawning a Nix
42+
shell:
11343

11444
```bash
115-
cd example # if not already in the example folder
116-
npm run build # builds to dist
117-
netlify deploy # deploy the dist folder
45+
nix-shell
11846
```
11947

120-
Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
48+
Now you should be able to clone the repo and install Node.js
49+
dependencies:
12150

12251
```bash
123-
netlify init
124-
# build command: yarn build && cd example && yarn && yarn build
125-
# directory to deploy: example/dist
126-
# pick yes for netlify.toml
52+
git clone https://github.com/soywod/react-use-bireducer.git
53+
cd react-use-bireducer
54+
yarn
12755
```
12856

129-
## Named Exports
57+
You can leave the development environment either by killing your
58+
terminal or by entering the command `exit`.
13059

131-
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
60+
## Tests
13261

133-
## Including Styles
62+
Tests are handled by [Jest](https://jestjs.io/) (`.test` files) and
63+
[React Testing
64+
Library](https://testing-library.com/docs/react-testing-library/intro/)
65+
(`.spec` files).
13466

135-
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
136-
137-
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
138-
139-
## Publishing to NPM
140-
141-
We recommend using [np](https://github.com/sindresorhus/np).
142-
143-
## Usage with Lerna
144-
145-
When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
146-
147-
The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
148-
149-
Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
150-
151-
```diff
152-
"alias": {
153-
- "react": "../node_modules/react",
154-
- "react-dom": "../node_modules/react-dom"
155-
+ "react": "../../../node_modules/react",
156-
+ "react-dom": "../../../node_modules/react-dom"
157-
},
67+
```bash
68+
yarn test
15869
```
15970

160-
An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64)
71+
## Sponsoring
72+
73+
[![github](https://img.shields.io/badge/-GitHub%20Sponsors-fafbfc?logo=GitHub%20Sponsors&style=flat-square)](https://github.com/sponsors/soywod)
74+
[![paypal](https://img.shields.io/badge/-PayPal-0079c1?logo=PayPal&logoColor=ffffff&style=flat-square)](https://www.paypal.com/paypalme/soywod)
75+
[![ko-fi](https://img.shields.io/badge/-Ko--fi-ff5e5a?logo=Ko-fi&logoColor=ffffff&style=flat-square)](https://ko-fi.com/soywod)
76+
[![buy-me-a-coffee](https://img.shields.io/badge/-Buy%20Me%20a%20Coffee-ffdd00?logo=Buy%20Me%20A%20Coffee&logoColor=000000&style=flat-square)](https://www.buymeacoffee.com/soywod)
77+
[![liberapay](https://img.shields.io/badge/-Liberapay-f6c915?logo=Liberapay&logoColor=222222&style=flat-square)](https://liberapay.com/soywod)

package.json

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
"name": "react-use-bireducer",
33
"author": "soywod <clement.douin@posteo.net>",
44
"description": "React hook for managing effects within reducers.",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"license": "MIT",
7-
"source": "src/index.ts",
8-
"exports": {
9-
"require": "dist/index.cjs",
10-
"default": "dist/index.modern.js"
11-
},
12-
"main": "dist/index.js",
13-
"module": "dist/index.esm.js",
14-
"unpkg": "dist/index.umd.js",
15-
"typings": "dist/index.d.ts",
7+
"source": "./src/index.ts",
8+
"exports": "./dist/index.mjs",
9+
"main": "./dist/index.js",
10+
"module": "./dist/index.esm.js",
11+
"unpkg": "./dist/index.umd.js",
12+
"typings": "./dist/index.d.ts",
1613
"files": [
17-
"dist"
14+
"./dist"
1815
],
1916
"devDependencies": {
2017
"@jest/types": "^28.1.0",
@@ -34,6 +31,7 @@
3431
"eslint-plugin-sort-keys-fix": "^1.1.2",
3532
"eslint-plugin-unused-imports": "^2.0.0",
3633
"husky": "^8.0.1",
34+
"jest": "^28.1.0",
3735
"jest-environment-jsdom": "^28.1.0",
3836
"lint-staged": ">=10",
3937
"microbundle": "^0.15.0",
@@ -57,8 +55,5 @@
5755
"build": "microbundle",
5856
"test": "yarn jest",
5957
"lint": "yarn eslint --ext ts src"
60-
},
61-
"dependencies": {
62-
"jest": "^28.1.0"
6358
}
6459
}

src/index.spec.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ type State = {
88
count: number;
99
};
1010

11-
type Action = {type: "increment"; value: number} | {type: "decrement"; value: number} | {type: "reset"};
11+
type Action =
12+
| {type: "increment"; value: number}
13+
| {type: "decrement"; value: number}
14+
| {type: "reset"};
1215

1316
type Effect = {type: "log"; value: string} | {type: "backup"; count: number};
1417

1518
const stateReducer: StateReducer<State, Action, Effect> = (state, action) => {
1619
switch (action.type) {
1720
case "increment": {
18-
return [{count: state.count + action.value}, [{type: "log", value: `increment counter +${action.value}`}]];
21+
return [
22+
{count: state.count + action.value},
23+
[{type: "log", value: `increment counter +${action.value}`}],
24+
];
1925
}
2026
case "decrement": {
21-
return [{count: state.count - action.value}, [{type: "log", value: `decrement counter -${action.value}`}]];
27+
return [
28+
{count: state.count - action.value},
29+
[{type: "log", value: `decrement counter -${action.value}`}],
30+
];
2231
}
2332
case "reset": {
2433
return [

0 commit comments

Comments
 (0)