npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wordpress-hooks

v1.0.2

Published

Custom hooks for React applications to communicate with WordPress APIs

Downloads

3

Readme

Wordpress Hooks

ReactPress is a collection of React hooks to easily fetch data from a WordPress REST API. It uses the React context API and the react-query library to fetch and manage the data.

Features

  • Simple to use React hooks for fetching WordPress data
  • Built-in caching and data management with react-query
  • Customizable API config

Installation

npm install reactpress

Usage

Wrap your app with the ReactPressProvider and provide an apiConfig object with the apiUrl property.

import { ReactPressProvider } from 'reactpress';

const apiConfig = {
  apiUrl: 'https://example.com/wp-json',
};

ReactDOM.render(
  <React.StrictMode>
    <ReactPressProvider apiConfig={apiConfig}>
      <App />
    </ReactPressProvider>
  </React.StrictMode>,
  document.getElementById('root')
);

Use the provided hooks to fetch WordPress data:

  • usePost
  • usePosts
  • usePage
  • usePages
  • useCategories
  • useTags
  • useMedia
  • useUsers
  • useComments
import { usePost, usePosts } from 'reactpress';

const Post = ({ id }) => {
  const { data: post, isLoading } = usePost(id);

  if (isLoading) {
    return <div>Loading...</div>;
  }

  return <h1>{post.title.rendered}</h1>;
};

const Posts = () => {
  const { data: posts, isLoading } = usePosts({ per_page: 5 });

  if (isLoading) {
    return <div>Loading...</div>;
  }

  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title.rendered}</li>
      ))}
    </ul>
  );
};

API

Hooks

All hooks accept an optional queryOptions object for filtering the results.

  • usePost(id) - Fetch a single post by ID or slug.
  • usePosts(queryOptions) - Fetch multiple posts.
  • usePage(id) - Fetch a single page by ID or slug.
  • usePages(queryOptions) - Fetch multiple pages.
  • useCategories(queryOptions) - Fetch multiple categories.
  • useTags(queryOptions) - Fetch multiple tags.
  • useMedia(queryOptions) - Fetch multiple media items.
  • useUsers(queryOptions) - Fetch multiple users.
  • useComments(queryOptions) - Fetch multiple comments.

ReactPressProvider

The ReactPressProvider component should be used to wrap your app and provide the apiConfig object. The apiConfig object must include the apiUrl property.

useReactPress

The useReactPress hook provides access to the ReactPress context. This can be useful for advanced usage or custom hooks.

License

This project is licensed under the MIT License.

Contributing

If you would like to contribute to this project, please follow these steps:

  1. Fork the repository on GitHub.
  2. Clone the forked repository to your local machine.
  3. Create a new branch for your feature or bugfix.
  4. Make your changes in the new branch.
  5. Commit and push your changes to the new branch.
  6. Open a pull request on the original repository, comparing your branch to the main branch.

Please ensure that your code follows the existing coding style, and make sure to include tests and documentation for any new features.

Support

If you encounter any issues or have questions, please open an issue on GitHub. We will do our best to address your concerns as quickly as possible.

Acknowledgements

This project was inspired by and built upon the concepts from the WordPress REST API and the react-query library.