JavaScript React Set State From CountDown Child react state from props,react state from child to parent,

I have a <CountDown/> component that I'd like to hold it's own logic so it can be reusable else where in the app.

I'm struggling to reason how could I setState to show:true on a sibling component i.e <List/> once the count has reached 0.

Currently, this is the hierarchy of the components:

export default class App extends Component {
  state = { show: false };

  render() {
    return (
      <div className="App">
        <Countdown />
        <List {...this.state} />
      </div>
    );
  }
}

I'd like to show the contents of a <List/>

const fruits = ["banana", "apple", "peach"];
export const List = ({ show }) => fruits.map(fruit => <li className={show ? "show" : "hide"}>{fruit}</li>);


Once the currentCount = 0

import React, { Component } from "react";

export default class Countdown extends Component {
  state = { currentCount: 3 };

  // decrement timer method
  timer() {
    this.setState({
      currentCount: this.state.currentCount - 1
    });
    //clear interval
    if (this.state.currentCount < 1) {
      clearInterval(this.intervalId);
    }
  }

  // decrement every second i.e 1000
  componentDidMount() {
    this.intervalId = setInterval(this.timer.bind(this), 1000);
  }

  // Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().
  componentWillUnmount() {
    clearInterval(this.intervalId);
  }
  render() {
    const { currentCount } = this.state;
    return <h1>{currentCount}</h1>;
  }
}

My struggle is that if I were to lift the state of currentCount to the main <App/> I'd lose the ability to control the <CountDown/> with its own state and lifecycle methods. Besides I'd like the CountDown to have its own set of logic so that It can be reusable and removable wherever I need in the app.

Question: How could I set the state of show (passed down as a prop) to true, once the countdown reaches 0?

Here's a code sandbox

Answer:1

Define a method in App that will set the state show to true:

onFinish = () => {
   this.setState({ show: true });
};

Now send it as a props to CountDown:

<Countdown onFinish={this.onFinish} />

Now call it once your local state reached zero:

if (this.state.currentCount < 1) {
    clearInterval(this.intervalId);
    this.props.onFinish();
}

Here is your Sandbox's fork

I also moved that last part of code on setState's callback because setState works in an asynchronous way.

Answer:2

I can save all data varchar/text element from my form, but I can't save my path image. Whats is wrong with my code? Lets see my create.blade.php I can save value of var deadline but I can't save ...

I can save all data varchar/text element from my form, but I can't save my path image. Whats is wrong with my code? Lets see my create.blade.php I can save value of var deadline but I can't save ...

  1. save path image android
  2. save path image
  3. save image path in database codeigniter
  4. save image path in database c#
  5. save image path in database php
  6. save image path in sqlite android
  7. save image path in database laravel
  8. save image path in database
  9. save image path in database asp.net c#
  10. save image path in database asp.net c# mvc
  11. save image path in mysql using php
  12. save image path in database c# windows application
  13. save image path in database java
  14. save image path c#
  15. save image path python
  16. save image path in database mvc
  17. save image path in database c# mvc
  18. save image path in mysql
  19. save image path in sharedpreferences
  20. save image path sql server

const A = [1,2,3,4,5] const B = [6,7,8,9,10] const C = []; for (let j=0; j<A.length;j++){ C[j] = C[[A[j],B[j]] } I want to create a 2D array C such that C = [[1,6],[2,7],[3,8],[4,9],[...

const A = [1,2,3,4,5] const B = [6,7,8,9,10] const C = []; for (let j=0; j<A.length;j++){ C[j] = C[[A[j],B[j]] } I want to create a 2D array C such that C = [[1,6],[2,7],[3,8],[4,9],[...

  1. create array from string
  2. create array from list
  3. create array from object
  4. create array from list python
  5. create array from number
  6. create array from 1 to n python
  7. create array from set javascript
  8. create array from string java
  9. create array from csv python
  10. create array from range vba
  11. create array from object values
  12. create array from string python
  13. create array from dataframe
  14. create array from 0 to n python
  15. create array from arraylist java
  16. create array from json
  17. create array from csv powershell
  18. create array from object keys
  19. create array from array javascript
  20. create array from string bash

I've got multiple items, some of them with the same title. I want to create an multidimensional array with the title as the first key and a unique number as the second key. So it's possible to ...

I've got multiple items, some of them with the same title. I want to create an multidimensional array with the title as the first key and a unique number as the second key. So it's possible to ...

  1. group array based on value javascript
  2. group array based on values php

So I have this example as shown below and I was wondering if its possible to translate a camera by changing the radius & diameter instead of using x,y,z positions (Vector). For now im using a cube ...

So I have this example as shown below and I was wondering if its possible to translate a camera by changing the radius & diameter instead of using x,y,z positions (Vector). For now im using a cube ...

  1. translating with camera
  2. translation with camera app
  3. camera translation in hindi
  4. camera translation in kannada
  5. camera translation and rotation
  6. camera translation from italian
  7. camera translation to japanese
  8. camera translation in different languages
  9. camera translation in spanish
  10. camera translation in nepali
  11. camera translation to russian
  12. chinese translator camera
  13. camera translation to arabic
  14. camera translation in swahili
  15. camera translation in malay
  16. translation for camera da letto
  17. camera translation in other languages
  18. camera translation to portuguese
  19. camera translation in gaelic
  20. text translate camera