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

Added react-copy-write example for react #59

Merged
merged 3 commits into from
May 25, 2018
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
5 changes: 5 additions & 0 deletions React/react-copy-write/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`
21 changes: 21 additions & 0 deletions React/react-copy-write/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "react-copy-write-rsm",
"version": "1.0.1",
"description": "React State Museum - Example of react-copy-write",
"main": "src/index.js",
"keywords": [],
"dependencies": {
"packlist-components": "^1.2.0",
"react": "^16.3.2",
"react-copy-write": "^0.1.0",
"react-dom": "^16.3.2",
"react-scripts": "^1.1.4"
},
"devDependencies": {},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
42 changes: 42 additions & 0 deletions React/react-copy-write/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
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>
16 changes: 16 additions & 0 deletions React/react-copy-write/src/Components/addItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import { AddPackingItem } from "packlist-components";
import { Consumer, addItem, setNewItemText, clearItems } from "../store";

export default () => (
<Consumer selector={state => state.newItem}>
{newItem => (
<AddPackingItem
addItem={addItem}
clear={clearItems}
setNewItemText={event => setNewItemText(event.target.value)}
value={newItem}
/>
)}
</Consumer>
);
13 changes: 13 additions & 0 deletions React/react-copy-write/src/Components/listItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { Component } from "react";
import { SimpleList } from "packlist-components";
import { Consumer } from "../store";

export default class ListItems extends Component {
render() {
return (
<Consumer selector={state => state.allItems}>
{allItems => <SimpleList value={allItems} />}
</Consumer>
);
}
}
29 changes: 29 additions & 0 deletions React/react-copy-write/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 AddItem from "./Components/addItem";
import { Provider } from "./store";

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

render(
<Provider>
<div style={styles}>
<h2>Welcome to React Copy Write!</h2>
<AddItem />
<ListItems />
</div>
</Provider>,
document.getElementById("root")
);
19 changes: 19 additions & 0 deletions React/react-copy-write/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import createState from "react-copy-write";

export const { Provider, Consumer, createMutator } = createState({
newItem: "",
allItems: ["Hot dog", "Hamburguer", "Pizza"]
});

export const setNewItemText = createMutator((draft, params) => {
draft.newItem = params;
});

export const addItem = createMutator(draft => {
draft.allItems = draft.allItems.concat([draft.newItem]);
draft.newItem = "";
});

export const clearItems = createMutator(draft => {
draft.allItems = [];
});
Loading