Understanding the dot notation and import syntax in JavaScript schemas.
9 replies
Last updated: Sep 16, 2022
R
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
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
and
I know I can just import
I’m just wondering what is the dot notation on the schema name, like in
name: navigation.sectionand 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, ... ])
linkor
section, but what is the purpose of it?
Sep 15, 2022, 10:37 PM
K
The dot notation is used for accessing an object keys. If you have an object
var obj = { foo: "bar" }, you can access the value
baras
obj.foo.
Sep 16, 2022, 6:41 AM
K
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
K
Ah wait, I’m talking about something else. I just checked the link you shared. 😄
Sep 16, 2022, 6:42 AM
K
I mean, everything I said is true, but that's not what's relevant here.
Sep 16, 2022, 6:42 AM
K
So in that article, the names like
navigation.linkare 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
K
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
navigationand its key
link. Which is not what is happening here.
Sep 16, 2022, 6:44 AM
K
Regarding the imports, you can name that variable whatever you want. It doens’t have to match the Sanity name. So:
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.
import navLink from './objects/navigationLink'
Sep 16, 2022, 6:45 AM
R
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!
Thank you!
Sep 16, 2022, 4:49 PM
R
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.