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

fix: store returning keys and empty objects #3

Open
w7tf opened this issue Oct 17, 2024 · 2 comments
Open

fix: store returning keys and empty objects #3

w7tf opened this issue Oct 17, 2024 · 2 comments

Comments

@w7tf
Copy link
Owner

w7tf commented Oct 17, 2024

Tested on iOS ⚠️

In mobile/app/comments/update/[commentId].tsx finding the comment returns empty objects.

const { commentId } = useLocalSearchParams<{
    commentId: string;
  }>();

  const router = useRouter();
  //   const comment = store$.comments.get().find((c) => c.id == commentId); // WORKS
  const comment = store$.comments.find((c) => c.id.get() == commentId); // Does not work

When trying to set to trigger an update sync it won't work because the objects are empty:

const handleCommentUpdate = () => {
    comment?.content.set(data.comment.get());
    comment?.author.set(data.author.get());
    router.back();
  };

LOG output of comment:

 LOG  {"author": {}, "content": {}, "createdAt": {}, "id": {}, "postId": {}, "updatedAt": {}}
@jmeistrich
Copy link

I think this only appears to be empty objects because it actually returns an observable. So just add a get() and it should work. But if you don't need the observable then the other way is more performant, to get the raw data first and then find it.

const comment$ = store$.comments.find((c) => c.id.get() == commentId)
const comment = comment$.get()

@jmeistrich
Copy link

Also if you're doing this kind of find often it may be better to use as: "object" since you get quickly get items by id and don't have to search the whole array.

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