score:-1
use an if statement to check if the object exists.
if(this.state.objectname)
delete this.state.objectname
score:0
how about toggling the state?
this.setstate(prevstate => ({
testobject: {
...prevstate.testobject,
someproperty: !prevstate.testobject.someproperty
}
}))
score:0
you can check it by this way:
if (this.state.testobject.hasownproperty(property_name))
delete this.state.testobject.property_name
else {
var testobject = this.state.testobject
testobject[property_name] = some_value
this.setstate({testobject: testobject})
}
score:0
this code will delete the testobject key from the state if present.
assuming that obj
is the object you want to delete/add.
let newstate = this.state.testobject
if (this.state.testobject.obj) {
delete newstate["obj"]
} else {
newobject["obj"] = "value"
}
this.setstate({ testobject: newstate })
if the obj
is present in testobject, then it will delete it, else it will insert obj
in testobject.
score:0
lets say the object in question is in testobject with key obj and the incoming var is newtestobj.
then we will check
if(this.state.testobject && this.state.testobject.obj) {
let tempobj = this.state.testobject;
delete tempobj.obj;
this.setstate({
testobject: tempobj
})
}else {
this.setstate({
testobject: {...testobject, obj: newtestobj.obj}
})
}
Source: stackoverflow.com
Related Query
- React - check if a object is in state variable
- Dynamically setting state object by passing it as variable name : React
- React state not setting Object from temporary variable
- React use object value as state variable name
- what is the right way to check that an object is not null in react before assigning one of its properties to a variable
- Setting a object array to a state variable in React
- Unable to define an imported object from a different file , store in a state and spread it in another variable in React
- Updating and merging state object using React useState() hook
- Storing an object in state of a React component?
- Check if variable is React node or array
- Using immutability-helper in React to set variable object key
- Jest react testing: Check state after delay
- How can I remove an attribute from a React component's state object
- How to pass a state className variable to another component in react
- Updating the array object in React state using immutability helper
- React hooks: How do I update state on a nested object with useState()?
- How to update object in React state
- Replace object in array on react state
- React Component check is variable is empty
- React PropType check dynamic object
- Updating the react component state when a global variable changes
- React state changes when its assigned variable changes
- React hooks state variable not updating after rerender
- Form created with React doesn't update the backing state object
- Change single variable of react state
- React Dev Tools does not show Component Names or State Variable Names
- Should I initialize a null state variable in a React component?
- Copying React State object; modifying copied object changes state object
- React idiomatic way to update Map object in state
- react how to call state variable from another class?
More Query from same tag
- How can we replace space in url with dash?
- Incorrect values on Highcharts (compared to input)
- .map not is not a function on empty object?
- Async data flow in React App with Redux + ReactRouter?
- How to pass params to an event handler within map function
- How to solve double API fetch on universal ReactJS
- How to make a layout with Styled Components?
- Pushing data from one component to a another
- function argument types when get a value from an HTML element on react
- React-query typescript useInfiniteQuery
- How to install SSL for the following setup (React Frontend + Nodejs Backend + Custom Domain Heroku)
- React setState() needs 2 clicks to update UI
- Jest React - Test suite failed to run, Cannot find module 'react-dotenv'
- Update JSX content in React Js with Hooks
- How can i pass the data from the response as a prop to a component?
- Fetch data in parallel using Redux Saga
- Nested flexbox is overflowing screen
- Why I my React Project goes into infinite loop and keep rendering pokemons on the window?
- I'm having a problem triggering this classlist toggle in React
- React/Typescript forwardRef types for an element which returns either an input or textArea
- A generic change handler for text inputs using Typescript/React with dictionary type
- How can I read metadata of PNG in react
- What is the best way to have variable attributes in JSX?
- How to open Drawer from another component
- How to render a component when state and props are changed?
- React/Styled-Components: Need to hand roll a dropdown with multiple versions that a developer could use by passing a type in
- Use dynamically created react components and fill with state values
- Changing Visibility of a button inside the component dynamically
- React.js server side rendering with PHP
- Re-render functional component when property of another class changes [MVVM]