Logesh Selva
Logesh Selva

Reputation: 79

how to set a timer for button in react native?

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

Answers (2)

Queens Coder
Queens Coder

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

Prasun
Prasun

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

Related Questions