Created
May 28, 2020 12:48
-
-
Save nestarz/04df865ba3fe4fd25f8171978d171160 to your computer and use it in GitHub Desktop.
Gun React Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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