score:2
in case that the property you are destructuring is not defined, you can assign "default values" like this:
const props = {diffuser: {name: "peter"}};
const { user: {name} = {name: "default-value"} } = props;
console.log(name);
the simpler example,
var { message: msg = "something went wrong" } = {};
console.log(msg);
a variable can be assigned a default,
in the case that the value unpacked from the object is undefined
.
score:0
you can just add a quick default value an check for undefined or null string after:
const { user: { name } = {} } = props;
this way it will not throw an error if 'user' is undefined, name will just be undefined also.
score:3
try this instead:
const {
user: { name = '' } = {name: ''}
} = props;
<div>{name}</div>
Source: stackoverflow.com
Related Query
- destructing nested object in react prop got error
- React Nested Object Gives Error
- Destructing an object and using it - React prop
- Dynamic nested object prop validation - react
- Access object nested in React object prop
- React nested json object error thiks it is object
- Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development
- React error 'Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement'
- React error: Style prop value must be an object react/style-prop-object
- Cannot convert object to primitive value error in react application?
- Expected onClick listener to be a function, instead got type object - react redux
- React hooks: How do I update state on a nested object with useState()?
- Why I got Error while Creating React App?
- Trying to deploy my React app with gh-pages but got this error message : The "file" argument must be of type string. Received type undefined
- Got an error Invalid src prop ('here is a link') on `next/image`, hostname "localhost" is not configured under images in your `next.config.js`
- React + Formik: Use value for nested object
- react pass a class object as prop
- React How to fix Failed prop type - Invalid prop of type string expected object
- In React how to add key prop to element passed as Object
- React Array converted to object when passed as a prop to SFC
- Implementing Google sign-in in React error - Invalid value for prop `data-callback` on <div> tag
- NextJS error message: Failed prop type: The prop `href` expects a `string` or `object` in `<Link>`, but got `undefined` instead
- React Native render error Value is undefined expected a object
- React SSR error -- Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
- Line clamp in React using clamp.js causing Object error in IE11
- React Router DOM History Object Causes Typescript Error
- React - how to map nested object values?
- Can't access nested object properties in a single object from JSON call in REACT
- connect react redux HOC got error of `Cannot call a class as a function`
- error "Converting circular structure to JSON -> starting at object with constructor 'FiberNode'" in react native
More Query from same tag
- Draggable Item from component A to B
- Testing React Component with Enzyme and Mocha
- Modify an HTML element from one component in another component in React.js
- Mongoose with mongodb how to return just saved object that have full or customize property with other collection?
- What is the difference between using constructor vs state = {} to declare state in react component?
- React-Native, problems importing and using ImageBackground
- typescript: How to define type as any part of enum?
- How to change the backgroundand text color of the navbar on scroll in react.js?
- How to add marker onClick and show my geolocation in google-maps-react?
- Rendering every iteration of an array sort in React
- How to force re-render if map value changes?
- How to handle asynchronous call in redux react application
- How to re-render checkbox state when mapping over state
- How to make loading screen in ReactJS web app like splash in React Native
- How to set a ref when using React.cloneElement on a Function component?
- Module build failed: Error: dyld: Library not loaded: /usr/local/opt/libpng/lib/libpng16.16.dylib
- Autoclose='outside' doesn't work with multiple menu in react-bootstrap
- react-bootstrap justify-content-end not being recognized when in desktop resolution, only when in mobile
- Calling a component method from another component when using react-redux ?
- Atomic Design: should a wrapper be an atom if it's not reusable?
- Passing props / calling functions in React
- React refs created from Array not working
- How to get ajax data in ReactJS
- Mapping JSON data to Chart.js Bar chart in React
- State attribute doesn't get updated when route param changes in React
- INTERNAL ASSERTION FAILED - firebase error in React JS while adding document to the collection
- Griddle v1 onRowClick not firing
- How do I get my dropdown in react to update without having to refresh?
- Redux Toolkit Migration
- Redux state is getting undefined