how to make component to reload , what I mean not re-render the component but I want to make componentDidMount
to be called again (re-start life cycle of component)
class Test extends Component
{
componentDidMount()
{
console.log('component did mount');
}
reload = () =>
{
//RELOAD COMPONENT
};
render()
{
return (<Button onPress={this.reload}/>)
}
}
If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page. import React from 'react'; function App() { function refreshPage() { window. location. reload(false); } return ( <div> <button onClick={refreshPage}>Click to reload!
This component is used inside a ScrollView or ListView to add pull to refresh functionality. When the ScrollView is at scrollY: 0 , swiping down triggers an onRefresh event.
Hot reloading allows you to see the changes that you have made in the code without reloading your entire app. Whenever you make any changes, all you need to do is save your code. As soon as you save your code, React Native tracks which files have changed since your last saved, and only reload those file for you.
Triggering a Child Component to Re-render To force the child component to re-render — and make a new API call — we'll need to pass a prop that will change if the user's color preference has changed. This is a simple switch we can flip.
You can call the life-cycle methods explicitly
reload = () =>
{
//RELOAD COMPONENT
this.componentDidMount();
};
If you want to re-start the life-cycle, you have to remove it from the DOM and re-add it.
What you want can be done. But it is not the best practice. Basically, you need to toggle the availability of your component from the parent component. As shown below, parent component has a state property to tract the availability of the child component.
class Parent extends Component {
constructor(props) {
super(props);
this.state ={
showChild : true
}
}
componentDidMount() {
console.log('Parent Mounted');
}
reloadChild = () => {
this.setState({
showChild : false
})
setTimeout(() => {
this.setState({
showChild : true
})
},100);
console.log("Reload Child Invoked")
}
render() {
return (
<div >
{this.state.showChild?
<Child reloadChild={this.reloadChild}/> : null
}
</div>
);
}
}
The Child component would look as below
class Child extends Component {
constructor(props) {
super(props);
}
componentDidMount(){
console.log('Child component Mounted');
}
componentWillUnmount(){
console.log('Child Component Unmounted');
}
onButtonClick = () => {
console.log("Button Clicked")
this.props.reloadChild();
}
render() {
return (
<button onClick={this.onButtonClick}>
Click Me
</button>
);
}
}
When you click on the button of the child component, it will call the method in parent Component which toggle the availability of the child component. Note that I've used setInterval in the reloadChild() method to toggle the availability. As I've said earlier, this is not the best practice. Just a workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With