🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Error with Dashboard Widgets in Sanity Studio V3

7 replies
Last updated: Dec 2, 2022
I'm learning about Dashboard Widgets.Why do I get a
Uncaught error: null is not an object (evaluating 'dispatcher.useState')
error with the following?What am I doing wrong? And why?


import React, {useState} from 'react'
import {LayoutConfig, DashboardWidget} from '@sanity/dashboard'
import {Flex, Button,  Stack, Text} from '@sanity/ui'

export const SampleWidget = (config: {layout?: LayoutConfig} = {}): DashboardWidget => {
  const [location, setLocation] = useState("")

  const getLocation = async () => {
    let api = await fetch('<http://api.open-notify.org/iss-now.json>')
    let apijson = await api.json()
    let location = `location is ${apijson.iss_position.latitude} ${apijson.iss_position.longitude}`
    setLocation(location)
    
    console.log(`[ location ]:`, location)
    return location
  }
  const Widget = () => {
    return (
      <Stack space={3} padding={4}>
        <Flex align={'center'}>
          <Text>The location is:</Text>
          <Text>{location}</Text>
        </Flex>

        <Button
          fontSize={2}
          style={{cursor: 'pointer'}}
          paddingY={2}
          text="Get location"
          tone="default"
          onClick={getLocation}
        ></Button>
      </Stack>
    )
  }

  return {
    name: 'sample-widget',
    component: Widget,
    layout: {width: 'medium'},
  }
}
Dec 1, 2022, 7:18 PM
Hmm, are you sure you're getting data back from your API call?
Dec 1, 2022, 8:26 PM
user M
yes. I get the error even if I stub out the api call.
Dec 1, 2022, 8:31 PM
What does your dashboard config look like?
Dec 1, 2022, 8:33 PM
plugins: [
    dashboardTool({
      widgets: [
        SampleWidget(), // dashboard widget in ./dashboard-widgets/sample-widget/
      ],
    }),
...
The widget renders properly if I remove
const [location, setLocation] = useState("")
and the
setLocation(location)
calls. But then how would I bind the result of the API call to an output?
Dec 1, 2022, 8:36 PM
Ah, you're in V3, then. We're handling questions for the preview here until full release.
Dec 1, 2022, 8:38 PM
user M
Will we be able to ask v3 questions in here after the 8th? The GitHub repo is understandably much less frequented than Slack. Many questions don't get any response there.
Dec 2, 2022, 2:24 PM
Yes, but it's possible that community support for V3 will have less traction than V2 questions for a bit until more folks have had experience with it.
Dec 2, 2022, 6:17 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?