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
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.
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 ...
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],[...
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 ...
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 ...