score:3
Accepted answer
create a variable containing the initial state, then use it to replace the current state when you want to clear, like the below example:
const initstate = {
text: '0',
array: [],
operators:['+']
}
...
constructor(props) {
super(props);
this.state = initstate;
this.display = this.display.bind(this);
this.clear = this.clear.bind(this);
this.calculate = this.calculate.bind(this);
}
...
clear(){
this.setstate(initstate);
}
Source: stackoverflow.com
Related Query
- React: How to totally and completely clear state
- React & Jest, how to test changing state and checking for another component
- React router and this.props.children - how to pass state to this.props.children
- How to set React component state and props from browser
- how to type state and dispatch for useReducer - typescript and react
- How to clear react state in modal after closing?
- how and when to call a react component methods after state change from redux
- How to do timeout and then clear timeout in react functional component?
- How to optimize React components with React.memo and useCallback when callbacks are changing state in the parent
- How can mock the value of a state and data in my react test
- How to test state update and component rerender after async call in react
- how to hide and show loading spinner - Activity Indicator react native, managing props and state
- How to test redux state update with react testing library and jest
- How do I keep state persistant using local storage, react hooks, and Context Provider
- React Native: Redux - How to properly update TextInput via event and have redux state update?
- React useReducer and context: how to provide state selectors?
- How does React update a component and its children after a state change?
- how to set react parent component state by a async function and then pass this state to child as props?
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- In React Hooks. How to set state to a random number and use that to display a string from an array
- How to toggle boolean state in React and TypeScript?
- How to store react component in state and pass a callback function
- React How to change state value from child to Parent using useState and useContext
- How to declare different state and props for subclasses on react with typescript
- How to run React useEffect only once and property update state with setState
- How can I update state in Redux - moving todo up and down - React
- How to set a loading state for react component while sorting and filtering data?
- How to use React Redux state for conditionally showing and Disabling a Button
- React Buttons and State - how to avoid too many States?
- How can I redirect to another component in react and pass the state that I set in the previous component?
More Query from same tag
- React 16.8 hooks - child component won't re-render after updating parent state
- Display custom messages for radio options in Ant Design form
- React/Leaflet-side-by-side Error: Map container is already initialized. #35
- React equivilant of `$(window).scrollTop()`
- Property 'map' does not exist on type 'List'
- how to set column name in mui datatables using object key
- Too many re-renders. React limits the number of renders to prevent an infinite loop. React Hook and ReactJS
- Manipulate array with react hooks
- React useEffect hook causes DOMException: User activation is required to request permissions
- react hooks and typescript - Property '***' does not exist on type 'never'
- Align items horizontally bootstrap
- How to query the newer version of SWAPI using REACT Graphql to get all people?
- Dynamic form returns the same value for the first and second input
- ReactJS and Less, set dynamic class
- Typescript - How Multiple Type situation in React
- Nginx reverse proxy multiple react apps
- React TypeError: this.setState is not a function
- Undefined props in componentDidMount
- add onChange to input in string in react
- how to decrement count value that is in a nested array within a reducer
- Text is not transitioning in with parent element, it just pops up immediately
- Passing array from one component to another through parent component, but it doesn't work on first render
- Window scroll event to check if the bottom of page is reached
- How should I setup the Enzyme Adapter config file with Jest?
- Recursively render list of items based on the API response
- encounter error with rendering album image
- variable undefined in useEffect
- styled-components: extend styles and change element type
- Apollo query loading is always false
- Getting TypeError: props.children is not a function when trying to set props.children