score:251
to ensure that the implicit browser state you mention and state of children is reset, you can add a key
attribute to the root-level component returned by render
; when it changes, that component will be thrown away and created from scratch.
render: function() {
// ...
return <div key={uniqueid}>
{children}
</div>;
}
there's no shortcut to reset the individual component's local state.
score:0
maybe you can use the method reset() of the form:
import { useref } from 'react';
interface props {
data: string;
}
function demo(props: props) {
const formref = useref<htmlformelement | null>(null);
function resethandler() {
formref.current?.reset();
}
return(
<form ref={formref}>
<input defaultvalue={props.data}/>
<button onclick={resethandler}>reset</button>
</form>
);
}
score:2
the approach where you add a key
property to the element and control its value from the parent works correctly. here is an example of how you use a component to reset itself.
the key is controlled in the parent element, but the function that updates the key is passed as a prop to the main element. that way, the button that resets a form can reside in the form component itself.
const innerform = (props) => {
const { resetform } = props;
const [value, setvalue] = usestate('initialvalue');
return (
<>
value: {value}
<button onclick={() => { setvalue('newvalue'); }}>
change value
</button>
<button onclick={resetform}>
reset form
</button>
</>
);
};
export const app = (props) => {
const [resetheuristickey, setresetheuristickey] = usestate(false);
const resetform = () => setresetheuristickey(!resetheuristickey);
return (
<>
<h1>form</h1>
<innerform key={resetheuristickey} resetform={resetform} />
</>
);
};
score:14
you should actually avoid replacestate
and use setstate
instead.
the docs say that replacestate
"may be removed entirely in a future version of react." i think it will most definitely be removed because replacestate
doesn't really jive with the philosophy of react. it facilitates making a react component begin to feel kinda swiss knife-y.
this grates against the natural growth of a react component of becoming smaller, and more purpose-made.
in react, if you have to err on generalization or specialization: aim for specialization. as a corollary, the state tree for your component should have a certain parsimony (it's fine to tastefully break this rule if you're scaffolding out a brand-spanking new product though).
anyway this is how you do it. similar to ben's (accepted) answer above, but like this:
this.setstate(this.getinitialstate());
also (like ben also said) in order to reset the "browser state" you need to remove that dom node. harness the power of the vdom and use a new key
prop for that component. the new render will replace that component wholesale.
reference: https://facebook.github.io/react/docs/component-api.html#replacestate
score:22
adding a key
attribute to the element that you need to reinitialize, will reload it every time the props
or state
associate to the element change.
key={new date().gettime()}
here is an example:
render() {
const items = (this.props.resources) || [];
const totalnumberofitems = (this.props.resources.noofitems) || 0;
return (
<div classname="items-container">
<paginationcontainer
key={new date().gettime()}
totalnumberofitems={totalnumberofitems}
items={items}
onpagechange={this.onpagechange}
/>
</div>
);
}
Source: stackoverflow.com
Related Query
- How can I reset a react component including all transitively reachable state?
- How can reset a React child component back to original state after it updates state in the parent
- How can I reset a component to it's initial state in react
- How can I block a React component to be rendered until I fetched all informations?
- How do I pass all state variables inside react hooks to my child component
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How can I initialize Redux state for React component on its creation?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How can parent component reset many children in React
- How can I redirect to another component in react and pass the state that I set in the previous component?
- How remember that a React hooks component is unmounted, so can avoid state changes?
- How can I show a react component in all routes except one?
- How can I change state of current map item in react component
- React: How can you pass all the state at once to the child component with Hooks?
- How can I create a counter as components to use multiple times and create one button to reset all the counter in React JS?
- How can I store all the dynamic values and attribute labels in a state in react js
- How can I make transient/one-off state changes to a React component from one of its ancestors?
- How can I change styling of all Material UI Typography components in single react component without applying a class to every element?
- React JSX How can I switch component state between two objects on a timer
- How can we reset the data and state of the mutation in RTK query , like reset function in React Query for mutation?
- How can I maintain state and "this" with react component functions
- How can i keep state in a React component using ES6
- How can i reset react form all input field?
- how can I set the reducer's return value to state object in component in react js
- My react component is failing due to component rendering before state is set. How can I resolve this issue?
- How to reset the state in react when component is rendered or shown?
- how to make Redux state reset every time React component starts rendering
- How can custom Hooks in React have different state names then the state name used by the functional component from which it was called?
- How can I prevent a React state update on an unmounted component in my integration testing?
- How can we use a value from component state while exporting in react
More Query from same tag
- how to make input type= file to accept only images React
- Tailwind is not refreshing when working with vite + react
- Cannot set state of multiple checkboxes?
- Dynamic Routing in gatsby
- How to set visibility to a redux form field on value change?
- Reading an environment variable in react which was set by docker
- reselect memoization ignore irrelevant updates
- How can I create a List of items from the API data I get?
- Change Material-UI outlined Chip focus and hover color
- During on submit of the form throws Objects are not valid as a React child
- React-router-dom v4, links in NavBar but routing in App.js
- React triggering navigation from nested components
- Looping through an object in react
- How do I create an interval API call in Nextjs?
- Accessing and showing modal located in another component
- I want to go though array and check if it matches with the values or not in React
- Close ReactModal from ReactComponent
- React Native firebase signInWithEmailAndPassword how to validate login
- Ant Design React. Bootstrap Grid "container" concept
- ReactJS: How do I pass data from JSON in local storage to a component?
- How do I upload multiple files to Firebase from React and get the file name?
- How to dispatch a thunk from a saga?
- Object component as Prop React Typescript
- Error when trying to rum npm start command
- How to improve setting state in ReactJS?
- Can I write a if/else statement for Axios Get Method if condition is not met?
- react-router v6: Navigate to a URL with searchParams
- Getting a 404 error when trying to get Express and React to connect
- How to hide scroll bar in react app with allowing scrolling?
- State not update when using setter and setInterval in useEffect