score:5
Accepted answer
yes, this is perfectly correct and valid.
i personally, like to create a const initialstate
to make it a little easier to read.
const example = (props) => {
const initialstate = props.bar ? props.bar._id : '';
const [foo, setfoo] = usestate(initialstate);
return { foo };
};
as far as accessing _id
when bar
is undefined. there are a few ways to solve that as well.
one of the new ways to do so is to use the new optional chaining operator ?.
and the nullish coalescing operator ??
together.
const initialstate = props.bar?._id ?? '';
if you don't have support for those new operators you could also use the ||
or operator like so.
const initialstate = (props.bar || {})._id || '';
Source: stackoverflow.com
Related Query
- Typescript not inferring correct type after type check inside a ternary operator
- Utilizing AND statement (two conditions) inside react ternary operator
- ternary operator inside map
- How to have two function calls inside ternary operator in JSX?
- Ternary operator inside an object in JS
- Ternary operator inside useState. Is it correct?
- Ternary the operator inside the button and call the function
- handling multiple useState inside conditional ternary
- Can't use if statement inside const declaration, but ternary operator works
- Ternary operator inside render is not behaving correctly -react
- Ternary operator not using the useState correctly
- How can I do a ternary operator inside a map with the mapping for the true condition but without the mapping for the false
- Can I use a ternary operator inside of an image tag?
- value is always empty inside ternary operator statement
- Add function call inside ternary operator in JSX
- map is not a function returning div inside ternary operator
- ReactJS: Map Function Inside Ternary Operator
- Call a react component inside a ternary operator
- multiple ternary operator inside a map
- Error while conditional rendering of state value using ternary operator inside jsx in Reactjs 18.0
- HTML Element inside a Ternary Operator in React?
- Ternary operator results in undefined useState after first render
- useState hook not updating when adding a variable fetched through ternary operator
- How to set Ternary Operator "Loading" Div inside React Component with Two Return statements?
- ternary operator in jsx to include html with react
- Correct way to type nullable state when using React's useState hook
- Ternary operator on style with React Js Es 6
- multiple condition in ternary operator in jsx
- Do I need to use the spread operator when using useState hook on object when updating?
- React className with ternary operator add class 'null'
More Query from same tag
- how to delete empty spaces in string in react/javascript
- React and Redux : How to show the action.type on the Components?
- React state/child update not behaving as I expected it to
- Material-UI Select shows the wrong value: More explanation in the post
- Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
- How to get the promise state when creating a new Auth user in firebase using javascript
- Values not being passed properly to a function
- I lost props after reloading the page in react
- Check New Route on componentWillUnmount?
- How Do I Pass State (Value) when Calling Component in React?
- React Hook Form Register Different Forms With Same Field Names
- How to redirect to completely new page after user click in react?
- Is Webpack smart enough to make jQuery lighter?
- Catch Pressable press with styled-components in React Native
- Fire an event via click of an element
- 'React' refers to a UMD global, but the current file is a module
- ReactJs how to get specific props data in child component
- Gatsby markdown frontmatter link becomes a relative file path after GraphQL static query
- Uploading image files to AmazonS3 using ReactJs
- Prevent enter key on html input field react.js
- AXIOS Async/Await How to best write the code?
- Attempted import error: 'persistor' is not exported from './redux/store'
- How to remove an item instantly from the UI which is in a useState array
- Ability to view my state using the browser dev tools
- Angular 5.0 change detection strategy VS React's change detection strategy
- Getting undefined on state when accessing from child to parent - React
- Triggering action on route change with react-router
- TailwindCSS fixed width property not working on resolution change
- How can I add transition effect to images when they show up?
- React page refresh when submit while I have a confirmation popup. It means the popup shows up and disappears