Gatsby plugin for babel optional chaining operator
npm install gatsby-plugin-babel-optional-chainingThis plugin enables the optional chaining operator (a?.b) TC39 proposal for Gatsby.
Install the plugin and its dependencies:
``bash`
npm i gatsby-plugin-babel-optional-chaining @babel/core @babel/plugin-proposal-optional-chaining
or
`bash`
yarn add gatsby-plugin-babel-optional-chaining @babel/core @babel/plugin-proposal-optional-chaining
Add the plugin to gatsby-config.js:
`js`
module.exports = {
plugins: [
// other plugins
'gatsby-plugin-babel-optional-chaining',
],
}
`js
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz; // 42
const safe = obj?.qux?.baz; // undefined
// Optional chaining and normal chaining can be intermixed
obj?.foo.bar?.baz; // Only access foo if obj exists, and baz ifbar
// exists
// Example usage with bracket notation:
obj?.['foo']?.bar?.baz // 42
``