1

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?

1

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.