Skip to content

React binding API Design #6

Description

@acrazing

Query

Old style

function BlogList({ authorId }) {
  const [blogList] = useSelector(selectBlogList(authorId))

  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(getBlogList(authorId))
  }, [authorId]);

  return blogList.map((blogId) => <BlogRow blogId={blogId} key={blogId} />
}

New style

function BlogList({ authorId }) {
  const [blogList, status /* loading, is initializing, etc */] = useQuery(queryBlogList(authorId))

  return blogList.map((blogId) => <BlogRow blogId={blogId} key={blogId} />
}

Mutation

Old style

function BlogRow({ blogId }) {
  const [blog] = useSelector(selectBlog(blogId))
  const dispatch = useDispatch();
  const handleDelete = async () => {
    await dispatch(deleteBlog(blogId))
    toast('Deleted')
  }

  return (
    <a href={`/blog/${blogId}`}>
      {blog.title} <button onClick={handleDelete}>Delete</button>
    </a>
  )
}

New style

function BlogRow({ blogId }) {
  const [blog] = useSelector(selectBlog(blogId))
  const handleDelete = useAction(deleteBlog(blogId), () => toast('Deleted'))

  return (
    <a href={`/blog/${blogId}`}>
      {blog.title} <button onClick={handleDelete}>Delete</button>
    </a>
  )
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions