Reputation: 79
I am new to react-native.I need to show a button only for 30 minutes.I don't have a clear solution on the internet about timers,any help would be appreciated. How do I set timers for a button to show only for 30 minutes & hide after it?
Upvotes: 1
Views: 3371
Reputation: 464
https://www.w3schools.com/howto/howto_js_countdown.asp
You could follow this for how to make a countdown. A crude approach would be to make your component stateful and keep track of the time. If the time is 30 minutes or over use a ternary to change the view and hide the button.
Upvotes: 0
Reputation: 5023
You can use setTimeout
function to control the visibility of the Button.
Sample code:
class SomeComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
isButtonVisible: true
};
}
componentDidMount() {
setTimeout(() => {
this.setState({ isButtonVisible: false });
}, 1000 * 60 * 30);
}
render() {
const { isButtonVisible } = this.state;
return (;
<View>
...
{
isButtonVisible && <Button .../>
}
</View>
)
}
}
Hope this will help!
Upvotes: 2