Skip to content

Instantly share code, notes, and snippets.

@nestarz
Created May 28, 2020 12:48
Show Gist options
  • Save nestarz/04df865ba3fe4fd25f8171978d171160 to your computer and use it in GitHub Desktop.
Save nestarz/04df865ba3fe4fd25f8171978d171160 to your computer and use it in GitHub Desktop.
Gun React Example
import React, { useEffect } from "react";
import { useGunState, useGunSetState } from "./utils/gun-hooks.js";
const session = new Date().toISOString();
const gun = window.Gun({ peers: ["http://127.0.0.1:8765/gun"] }).get(100 * 1);
export default () => {
const [set, setSet] = useGunSetState(gun);
const [lock, setLock] = useGunState(gun.get("locked"));
const add = () => setSet("hello" + Math.random());
return !lock ? (
<>
{set.map(({ data, remove }) => (
<>
<div>{data}</div>
<button onClick={remove}>Remove</button>
</>
))}
<button onClick={add}>Add</button>
<button onClick={() => setLock(!lock)}>Lock</button>
</>
) : (
set.map(({ data }) => <div>{data}</div>)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment