npx create-react-app my-app --template typescript
cd my-app-name
npm install @fluid-experimental/fluid @fluid-experimental/data-objects
KeyValueDataObject will provide you with a fully scaffolded DDS to store your data and subscribe to listen for changes.
Fluid gives you access to methods that will bootstrap a new Fluid container, and we use the getContainerId
helper method to simplify the creation and sharing of multi-author, Fluid sessions.
import { KeyValueDataObject } from "@fluid-experimental/data-objects";
import { Fluid, getContainerId } from "./fluid";
In this simple multi-user app, we are going to build a button that, when pressed, shows the current time stamp. This allows co-authors to see the most recent timestamp at which the other authors pressed the button.
Remove all of the existing returned markup and replace it as shown below.
You can see that this UI requires a data
object and setData
functions to work, so we'll add those above and pull them out of a special React function we'll call useKVPair
.
function App() {
const { data, setData } = useKVPair();
return (
<div className="App">
<button onClick={() => setData("date", Date.now().toString())}>
click
</button>
{data && <span>{data.date}</span>}
</div>
);
}
Working in React, one of the best ways to abstract complex, reusable functionality is via a custom hook. Custom hooks are functions that have access to the built in React hooks like useState
and useEffect
which we'll need in order to load our Fluid DataObject, and track our local state.
const useKVPair = () => {};
- should we just switch to resolve for Containers? Commonly requested
- Does having just a getDataObject oversimplify things? Is depth there?
- Could you run getDataObject (w/ createnew) 2x w/ diff DO's on the same container?
- How do we simplify the useKVPair export to have more keyvalue semantics?
- Does the useKVPair method need to be performant?
- Can the useKvPair.Get require O(N) work?
- Can the UsekvPair.Set always trigger an entire dom refresh?
- Can you ugprade to a new syntax once our limtied perf runs out?
- How far does this need to scale with this syntax?
- If our funnel (CRA getting started app) is really simple, does the eventual use case need to follow the same syntax?
- We have to store all the data in one key value pair
- All of the data currently needs to be updated at once (you can't sepeartely collaborate on different parts)
- As currently written, it appears that we'd need to translate the Map object into a JS native object
- How do we have multiple KV pairs in this example? (May be optional)
- Ideally values are sub 1KB
- Redux model... getter/setter per key on the keyvalue
- then we don't have all of our data in one key/value pair
- Pass the MapKernal.data Map() into setState (is this possible?)