Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds zustand – React version #336

Merged
merged 2 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
| [overmind](https://overmindjs.org/?view=react&typescript=false) | [<img src="./_art/csb.png" width="40px" />](https://codesandbox.io/s/github/GantMan/ReactStateMuseum/tree/master/React/overmind) [<img src="./_art/octo.png" width="36px" />](https://github.com/GantMan/ReactStateMuseum/tree/master/React/overmind) | Coming Soon - Contribute Here? |
| [Venti](https://github.com/will123195/venti#readme) | [<img src="./_art/csb.png" width="40px" />](https://codesandbox.io/s/github/GantMan/ReactStateMuseum/tree/master/React/venti) [<img src="./_art/octo.png" width="36px" />](https://github.com/GantMan/ReactStateMuseum/tree/master/React/venti) | Coming Soon - Contribute Here? |
| [Recoil](https://github.com/facebookexperimental/Recoil) | [<img src="./_art/csb.png" width="40px" />](https://codesandbox.io/s/github/GantMan/ReactStateMuseum/tree/master/React/recoil) [<img src="./_art/octo.png" width="36px" />](https://github.com/GantMan/ReactStateMuseum/tree/master/React/recoil) | Coming Soon - Contribute Here? |
| [zustand](https://zustand.surge.sh) | [<img src="./_art/csb.png" width="40px" />](https://codesandbox.io/s/github/GantMan/ReactStateMuseum/tree/master/React/zustand) [<img src="./_art/octo.png" width="36px" />](https://github.com/GantMan/ReactStateMuseum/tree/master/React/zustand) | Coming Soon - Contribute Here? |


### Examples to help portray the how, why, which, pros, and cons of various state management systems in the React ecosystem.
Expand Down
23 changes: 23 additions & 0 deletions React/zustand/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions React/zustand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
If you use yarn, run:
`yarn && yarn start`

If you use npm, run:
`npm i && npm run start`
16,927 changes: 16,927 additions & 0 deletions React/zustand/package-lock.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions React/zustand/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "react-zustand-example",
"version": "0.1.0",
"description": "React State Museum - Example of zustand",
"dependencies": {
"packlist-components": "1.2.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"zustand": "^3.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
43 changes: 43 additions & 0 deletions React/zustand/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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>
26 changes: 26 additions & 0 deletions React/zustand/src/Components/addItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from "react";
import { useUserInputStore, useItemsStore } from "../store";

import { AddPackingItem } from "packlist-components";

const AddItems = () => {
const { input, setInput, resetInput } = useUserInputStore();
const addListItem = useItemsStore((state) => state.addItem);
const resetItem = useItemsStore((state) => state.resetItems);

const addItem = () => {
addListItem(input);
resetInput();
};

return (
<AddPackingItem
value={input}
setNewItemText={e => setInput(e.target.value)}
addItem={addItem}
clear={resetItem}
/>
);
};

export default AddItems;
10 changes: 10 additions & 0 deletions React/zustand/src/Components/listItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { SimpleList } from "packlist-components";
import { useItemsStore } from "../store";

const ListItems = () => {
const { items } = useItemsStore();
return <SimpleList value={items} />;
};

export default ListItems;
29 changes: 29 additions & 0 deletions React/zustand/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/************************************************
* This example is one of many examples
* -= The React State Museum =-
* View other state management options and read
* more in the blog post and the master repo:
*
* https://github.com/GantMan/ReactStateMuseum
************************************************/
import React from "react";
import { render } from "react-dom";
import ListItems from "./Components/listItems";
import AddItems from "./Components/addItem";

const styles = {
fontFamily: "sans-serif",
textAlign: "center",
};

function App() {
return (
<div style={styles}>
<h2>Welcome to zustand</h2>
<AddItems />
<ListItems />
</div>
);
}

render(<App />, document.getElementById("root"));
15 changes: 15 additions & 0 deletions React/zustand/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import create from "zustand";

const useItemsStore = create((set) => ({
items: ["nachos", "burritos", "hot dog"],
addItem: (newItem) => set((state) => ({ items: [...state.items, newItem] })),
resetItems: () => set({ items: [] }),
}));

const useUserInputStore = create((set) => ({
input: "",
setInput: (input) => set({ input }),
resetInput: () => set({ input: "" }),
}));

export { useItemsStore, useUserInputStore };
Loading