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

User request: Solid sample #920

Closed
aboodman opened this issue Apr 6, 2022 · 3 comments
Closed

User request: Solid sample #920

aboodman opened this issue Apr 6, 2022 · 3 comments

Comments

@aboodman
Copy link
Contributor

aboodman commented Apr 6, 2022

This one is interesting because of Solid's approach to state management which is a lot more principled than React's.

The problem with Replicache from Solid's point of view is that whenever a subscription changes, all dependent UI will re-render, whereas Solid has better capabilities built-in and could do far better with more detailed information.

Replicache knows which keys and values in particular changed, so if we could link these things up it could be pretty great. I think that #839 is probably a part of the solution here.

@arv
Copy link
Contributor

arv commented May 12, 2022

And here is a SolidJS sample:

import { Component, createSignal, For, onCleanup } from 'solid-js';

import {
  JSONValue,
  Replicache,
  TEST_LICENSE_KEY,
  WriteTransaction,
} from 'replicache';

const mutators = {
  addData: async (
    tx: WriteTransaction,
    { key, value }: { key: string; value: JSONValue },
  ) => {
    await tx.put(key, value);
  },
};

type M = typeof mutators;

const App: Component = () => {
  const rep = new Replicache({
    name: 'solid',
    licenseKey: TEST_LICENSE_KEY,
    mutators,
  });

  const [items, setItems] = createSignal<string[]>();

  const unsubscribe = rep.subscribe(
    tx => tx.scan({ prefix: 'message/' }).values().toArray(),
    { onData: setItems },
  );
  onCleanup(unsubscribe);

  let count = 0;
  const addItem = async () => {
    rep.mutate.addData({
      key: `message/${++count}`,
      value: `Hello World (${count})`,
    });
  };

  return (
    <div>
      <button onClick={() => addItem()}>Add Data</button>
      <For each={items()}>{item => <div>{item}</div>}</For>
    </div>
  );
};

export default App;

@arv
Copy link
Contributor

arv commented May 12, 2022

This does not use watch so it is not efficient but I still wanted to play with it.

@aboodman
Copy link
Contributor Author

Promoting to more of official support #1014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants