score:8
Accepted answer
constructor(props) {
super(props);
this.state = { timer: 30 };
}
componentDidMount() {
this.clockCall = setInterval(() => {
this.decrementClock();
}, 1000);
}
componentWillUnmount() {
clearInterval(this.clockCall);
}
decrementClock = () => {
this.setState((prevstate) => ({ timer: prevstate.timer-1 }));
};
You can create a countdown with setInterval
. Every second this.decrementClock()
will be called after the component mounts.
You will want to render this.state.timer
in your render method.
constructor(props) {
super(props);
this.state = { timer: 30 };
}
startTimer = () => {
this.clockCall = setInterval(() => {
this.decrementClock();
}, 1000);
}
decrementClock = () => {
if(this.state.timer === 0) clearInterval(this.clockCall)
this.setState((prevstate) => ({ timer: prevstate.timer-1 }));
};
componentWillUnmount() {
clearInterval(this.clockCall);
}
In render add the button with Onpress
event listener.
<button Onpress={this.startTimer}> Play </button>
<Text style={{fontSize: 18, color: '#000'}}>
{this.state.timer === 0 ? 'Times Up!' : {this.state.timer} }
</Text>
score:2
To prevent negative numbers try this:
decrementClock = () => {
this.setState((prevstate) => ({
timer: prevstate.timer-1
}), () => {
if(this.state.timer === 0) {
clearInterval(this.clockCall)
}
})
}
Source: stackoverflow.com
Related Query
- How i can create the function for the countdown in React native?
- How can we reset the data and state of the mutation in RTK query , like reset function in React Query for mutation?
- how to create React Context for a function value without re-rendering all the time
- How can I create multiple instances of a React component (having an infinite animation) with different values for the :root variables
- How can I make the function halt execution for n seconds in react js?
- How can i create a table using the react-pdf library for generation pdf report?
- For a React app, can the build process not create a JavaScript map file?
- How to enable the Android monitor in React Native for checking logs?
- How can I change the format for react datepicker days of the week?
- How can I create a progress bar for an API request in React Native?
- How to create type definition for the React useReducer hook action in Typescript?
- How can I replace screen with React Navigation for React Native
- How can I use an IIFE in the return function of a react component?
- How can I define the type for a <video> reference in React using Typescript?
- How to use React.FC<props> type when the children can either be a React node or a function
- How can ı pass function with state for react useContext
- How can I reuse the API data for all the pages in react js
- How can I programmatically set the user details for Launch Darkly in my react app
- How can i dispatch an action from a helper function in react native
- How to apply react-native-linear-gradient for the entire app background with React Native
- how can I create a react component for paypal in-context express checkout?
- React Native - how can I pass a function to a stack screen in a navigator?
- Support for the experimental syntax 'decorators-legacy' isn't currently enabled How To Enable In Create React App?
- How can I change the icon in material-ui date picker for React
- How can we write a click handler for a Stateless Function Component in React JS
- How can I create a reusable function that will setState for a variable object property?
- how to pass the current item to the function so that i can remove it from array in react component?
- How Can I refactor my App.js page routes in React ? What is the best practice for that?
- How can I check a function for having arguments without checking their values, using jest & enzyme in react component?
- How can I access another function inside the react Context.Provider value?
More Query from same tag
- React: How to use multiple Contexts of the same type while allowing children to read data from all of them
- antd Switch checked property
- Name webpack chunks from react-loadable
- In Webpack 4, can we dynamically generate page chunk with import() token so we can turn a react component into a react-loadable one?
- Cntrl S to submit a from in react JS but input field not working
- How to enable another input field when a user selects an option from dropdown in React
- React.js animate slide height when using adaptiveHeight
- React Component does not re-render on state change?
- How do i divide code into separate files?
- Go back to last page on another tab JS
- Module not found: Can't resolve 'styled-component'
- Mobx store in react doesn't rerender components
- Redux Form always validates even on a normal button press
- How do you use Framer Motion + Styled Components together?
- React's setState() not working for nested DOM elements
- 'this' inside a function of a class component (react)
- React error boundary gets invoked but does not render
- ReactJS: element isn't updated when state changes
- User Logout redirect using this.props.history.push('/') does not redirect to desired page
- React + spring boot : Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'
- react material-ui upload button look weird
- Trying to fetch random words from API and displaying them
- Is it possible to use the touch ripple effect of MUI on a div?
- How can I change the position of the side bar to right?
- pushing the specific table row data to a dialog in reactJS
- How to retrieve string after @ character and before space using javascript or react?
- How to make the Antd tooltip box disappear after hovering over it?
- ReactJS - TypeError: Cannot read property '0' of undefined
- How to make parent's width wrap its content in React Native?
- Can I render multiple element in a ternary condition without else?