score:-1
you need to set the proper value to the state, please check below code
newhandlechange=(value)=>{
localstorage.setitem("currenttime",value.format('h:mm a')) // if you really needs the local storage.
this.setstate({
value: value.format('h:mm a')
})
}
score:-1
try to used this `
newhandlechange =val=> {
const value=localstorage.setitem('currenttime', val.format('h:mm a'))
this.setstate({
value
})
}`
look to this this app https://codesandbox.io/s/competent-bohr-6jon1
score:1
when initializing state
in constructor
you should pass props
to super
:
constructor(props){
super(props);
this.state={
value : ''
}
}
the undefined
you're getting is just the result from getitem()
score:2
the reason why you're getting undefined
is because you're setting new state with the value that gets returned from calling localstorage.setitem
, which is undefined
.
here's what you need to do instead:
class time extends component {
constructor(props) {
super(props)
this.state = { value: '' }
}
// ...
newhandlechange = (value) => {
const time = value.format('h:mm a')
localstorage.setitem('currenttime', time)
this.setstate({
value: time
})
}
}
this way, you're saving your new time into a variable and using it to update currentvalue
in localstorage
and state
in your component.
Source: stackoverflow.com
Related Query
- React : state object property is not updating
- React not updating state with setState with Array of object
- Updating State | object is not iterable (cannot read property Symbol(Symbol.iterator))
- How add new object property while updating state in React
- useState in react is not updating the state object
- Not able to target a sub property of a JSON object in react stored in state within the render method
- React state - updating property on object in nested array
- React not updating property from an object inside an array
- React Component does not re rendering when updating an object in state hooks
- React set state not updating object data
- LocalStorage not updating property in state with React hooks
- React Hooks, object state is not updating onSubmit
- State not updating when using React state hook within setInterval
- React setState not updating state
- React Child Component Not Updating After Parent State Change
- Updating and merging state object using React useState() hook
- React component not updating when store state has changed
- Updating the array object in React state using immutability helper
- React : cannot add property 'X', object is not extensible
- Why is my react component not updating with state updates?
- State using React Typescript: Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes
- Props not updating when redux state change in React Hooks
- React hooks state variable not updating after rerender
- react is not updating functional component state on input change
- React Hook useCallback not updating State value
- React setState() not updating state after $.ajax() request
- Changing object property in an array with React Hooks does not evoke a re-render
- React Child with useEffect() not updating on Parent State Change
- React componentDidMount not updating the state
- React is not updating when Redux state is changed below the first level
More Query from same tag
- Detecting which node is selected within a slate-react text editor
- SWAPI does not return all people after selecting a film
- Access a page only if a user has admin role (React, Express, NodeJS, MongoDB)
- Showing users nearby locations based off radius
- How can I fire a react function from a ajax request success function?
- How to update item quantity in the shopping cart using React JS
- Display images with a loop in a React component
- react/webpack/django - uncaught typeerror: cannot read property 'yyy' of undefined
- Open Dialog From Other Page In React
- Infinite loop with redux-saga
- Why is getDerivedStateFromProps is a static method?
- Type 'string[]' is not assignable to type 'never[]'. Typescript Error
- How to return a component/component-tag dynamically in vue/nuxt?
- How to use leader-line (an external javascript library) in react?
- create environment variables based on the webpack entry point
- Background image with gradient overlay React
- Clear data on empty input - React.js
- Why have I errors while reading and writing from firebase in reactjs?
- How to show the progress bar when submitting the form in javascript-ajax
- Value of this in React event handler
- Typescript "Cannot assign to property, because it is a constant or read-only" even though property is not marked readonly
- How to use Typescript Union Type in React PropTypes?
- Calling a Component Function After Document-Wide KeyPress (React)
- Error: Element type is invalid: expected a string (for built-in components) reactjs
- React Search or Filter data based on multiple input
- What is the type of theme in Material-UI version 5?
- How to extend a react component from a library
- React code splitting to components app filters, sorts, adds, edits, removes data
- ES6 Arrow function without curly braces
- Storing image in base64 format in MERN stack