Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reload component - React

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}/>)
    }

}
like image 560
Ali Faris Avatar asked Nov 26 '17 06:11

Ali Faris


People also ask

How do I reload component react?

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!

How do you refresh a component in react native?

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.

What is hot reloading in react?

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.

How do you reload child component react?

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.


2 Answers

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.

like image 98
Kunukn Avatar answered Oct 09 '22 21:10

Kunukn


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.

like image 45
Sajith Edirisinghe Avatar answered Oct 09 '22 19:10

Sajith Edirisinghe