Skip to content

Commit

Permalink
add example project for zustand devonly devtools with better rewind f…
Browse files Browse the repository at this point in the history
…eature of redux devtools
  • Loading branch information
pavlobu committed Nov 17, 2022
1 parent 2b29d73 commit a233f1f
Show file tree
Hide file tree
Showing 29 changed files with 811 additions and 22 deletions.
2 changes: 2 additions & 0 deletions examples/dev-env-devtools-many-stores/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this vriable is ued by devOnlyDevtools export from zustand middleware
REACT_APP_CUSTOM_NODE_ENV = "development"
2 changes: 2 additions & 0 deletions examples/dev-env-devtools-many-stores/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# this vriable is ued by devOnlyDevtools export from zustand middleware
REACT_APP_CUSTOM_NODE_ENV = "production"
89 changes: 89 additions & 0 deletions examples/dev-env-devtools-many-stores/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Better zustand devtools by @pavlobu

This demo shows you how to use redux devtools with @pavlobu/zustand-middleware.

This project is based on Create React App.

## Note on devOnlyDevtools() usage

Use this `import { devOnlyDevtools } from '@pavlobu/zustand` export instead of standard `devtools` export of zustand.
This helps to remove react devtools in prod build with less hustle.

#### A small react refresher:

- When running `npm run build` `NODE_ENV` is `production`. But
- when running `npm run start` `NODE_ENV` is `development`.

#### _This may be not flexible in some cases._

For example if you need to have a non-prod build of this react on lower environments
`dev`, `qa`, `uat` etc.
But we have a solution. In this demo we are using `npm run build:dev` and `npm run build:prod`
commands that read `.env.production` and `.env.development` files.

The important variable in those `.env.*` files is `REACT_APP_CUSTOM_NODE_ENV`, if it's set
and it's value is `production` then devtools will be excluded, so you don't have to remove devOnlyDevtools() middleware wrapper on all of your zustand stores for prod build.
This variable is handled by `@pavlobu/zustand` package internally. But you don't have to create these `.env.*` files
with this `REACT_APP_CUSTOM_NODE_ENV` variable for basic usage.

### _Simple `npm run build` will also remove redux devtools middleware, because it's treated as prod build by default._

---

## Demo of redux devtools rewind feature

In this demo you can see a Redux Devtools Extension state rewind feature in action, when multiple zustand stores connected to one Redux Devtools Extension connection.
The code for this demo gif is in this current project. Here is the [link](https://github.com/pavlobu/zustand/tree/%40pavlobu-zustand-devtools-middleware/examples/dev-env-devtools-many-stores) in case if you need it

![demo of zustand devtools middleware by pavlobu. showing how redux devtools rewind feature works](https://github.com/pavlobu/zustand/blob/e0ffeebebfb825f30c36992f2110f978f4f44c93/examples/dev-env-devtools-many-stores/docs/img/zustand-devtools-rewind.gif)

---

---

# Getting Started with this demo project

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions examples/dev-env-devtools-many-stores/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "zustand-bears-redux-devtools",
"version": "0.1.0",
"private": true,
"dependencies": {
"@pavlobu/zustand": "^4.1.4-1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.11.59",
"@types/react": "^18.0.20",
"@types/react-dom": "^18.0.6",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.8.3",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build:dev": "env-cmd -f .env.development react-scripts build",
"build:prod": "env-cmd -f .env.production react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint ./src --ext .js,.jsx,.ts,.tsx --fix --max-warnings=0"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/lodash": "^4.14.185",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"env-cmd": "^10.1.0",
"eslint": "^8.23.1",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.0.4",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-react": "^7.31.8",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
"eslint-plugin-unused-imports": "^2.0.0"
},
"lint-staged": {
"*.{jsx,ts,tsx}": "eslint --max-warnings=0"
}
}
Binary file not shown.
44 changes: 44 additions & 0 deletions examples/dev-env-devtools-many-stores/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="%PUBLIC_URL%/static/env-current.js?%REACT_APP_VERSION%" type="text/javascript"></script>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions examples/dev-env-devtools-many-stores/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions examples/dev-env-devtools-many-stores/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ENVIRONMENT = 'local'

// eslint-disable-next-line no-console
console.log('INFO: Env Loaded current')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVIRONMENT = 'development'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVIRONMENT = 'local'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVIRONMENT = 'development'
66 changes: 66 additions & 0 deletions examples/dev-env-devtools-many-stores/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.App {
margin: 2em;

}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

p {
text-align: center;
font-size: 2rem;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.actions {
display: flex;
justify-content: space-around;
}

button {
font-family: monospace;
font-size: large;
color: azure;
padding: 15px;
background-image: linear-gradient(to top, #09203f 0%, #537895 100%);
border-radius: 5px;
border-style: none;
cursor: pointer;
}

span {
margin: 3vw;
font-size: 5rem;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
50 changes: 50 additions & 0 deletions examples/dev-env-devtools-many-stores/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable react/no-multi-comp */
import './App.css'
import {
AddBear,
AddBee,
NukeBears,
NukeBees,
RemoveBear,
RemoveBee,
ShowBears,
ShowBees,
SpecificBearsAmount,
SpecificBeesAmount,
} from './components/ActionButtons'
import { Bears } from './components/Bears'
import { Bees } from './components/Bees'
import { useBearsStore } from './components/Bears/bear-store'
import React from 'react'

const App = () => {
const isZeroBears = useBearsStore((state) => state.bears) === 0
const isZeroBees = useBearsStore((state) => state.bears) === 0

return (
<div className="App">
<ShowBears />
<div className="actions">
{isZeroBears ? null : <RemoveBear />}
<AddBear />
{isZeroBears ? null : <NukeBears />}
<SpecificBearsAmount />
</div>
<br />
<Bears />
<br />
<ShowBees />
<div className="actions">
{isZeroBees ? null : <RemoveBee />}
<AddBee />
{isZeroBees ? null : <NukeBees />}
<SpecificBeesAmount />
</div>
<br />
<Bees />
</div>
)
}

// eslint-disable-next-line import/no-default-export
export default App

0 comments on commit a233f1f

Please sign in to comment.