👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Understanding the dot notation and import syntax in JavaScript schemas.

9 replies
Last updated: Sep 16, 2022
Hi all. I have a bit of a newbie question, mostly related to JS. I decided to use this snippet https://www.sanity.io/schemas/nested-navigation-structure-757f39ee
I’m just wondering what is the dot notation on the schema name, like in
name: navigation.section
and how do I import those? is it namespacing?
I ask because I can’t find anything about it on the documentation and VSCode is yelling at me when I try to import for example


import navigation.link from './objects/navigationLink';

and


  types: schemaTypes.concat([
    ...
    navigation.link,
    ...
  ])
I know I can just import
link
or
section
, but what is the purpose of it?
Sep 15, 2022, 10:37 PM
The dot notation is used for accessing an object keys. If you have an object
var obj = { foo: "bar" }
, you can access the value
bar
as
obj.foo
.
Sep 16, 2022, 6:41 AM
So you can’t import a key from an object. You need to import the whole thing, and then you can read its keys (if it's an object).
import navigation from './objects/navigation'

navigation.link
Sep 16, 2022, 6:41 AM
Ah wait, I’m talking about something else. I just checked the link you shared. 😄
Sep 16, 2022, 6:42 AM
I mean, everything I said is true, but that's not what's relevant here.
Sep 16, 2022, 6:42 AM
So in that article, the names like
navigation.link
are just made this way for clarity. It‘s a convention, if you will. It’s just a name as a string. It could contain anything, and could just as well be
navigationLink
.
Sep 16, 2022, 6:43 AM
So it’s
name: "navigation.link”
, not
name: navigation.link
. That’s an important difference, because the first one is a literal string, while the second one is attempting to read an object
navigation
and its key
link
. Which is not what is happening here.
Sep 16, 2022, 6:44 AM
Regarding the imports, you can name that variable whatever you want. It doens’t have to match the Sanity name. So:
import navLink from './objects/navigationLink'
Indeed, variables can‘t contain dots in JS (because of the whole object thing I explained above), that's why your code is throwing an error.
Sep 16, 2022, 6:45 AM
The quotes around the name and the naming of the import variables whatever I wanted were the key here. Thank you so much! That dot naming convention threw me totally off!
Thank you!
Sep 16, 2022, 4:49 PM
user M
for future reference 👆
Sep 16, 2022, 4:50 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?