New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mobx-with-query

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-with-query

Fetching server state in your MobX-store

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

WithQuery

It makes fetching and updating server state in your MobX-store a breeze.

Usage

import { WithQuery } from "mobx-with-query"

class Store {
  constructor() {
    makeAutoObservable(this)
  }

  data = new WithQuery(() =>
    fetch("https://api.github.com/repos/tannerlinsley/react-query"),
  )
}

In your React-component:

const Page = observer(() => {
  const [store] = useState(() => new Store())
  const { isLoading, data, error } = store.data

  if (isLoading) return "Loading..."

  if (error) return "An error has occurred: " + error.message

  return <div>{data.name}</div>
})

Config

The onError and onSuccess can be used to handle these events:

class Store {
  data = new WithQuery(Api.getMethod, {
    onError: () => alert("Server Error. Please try again later"),
    onSuccess: () => alert("Completed successfully"),
  })
}

You can disable automatic data loading using loadOnMount. In addition, you can transform the response using mapping:

class Store {
  project = new WithQuery(Api.getProject, {
    loadOnMount: false,
    transform: (data) => data?.name.toUpperCase(),
  })
}

const Page = observer(() => {
  const [store] = useState(() => new Store())
  const { id } = useParams()

  useEffect(() => {
    project.load(id)
  }, [id])
})

Keywords

FAQs

Package last updated on 21 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc