Skip to content
the poor coder

setstate to false after 10 sec react native

Poor Man
Published
Reading time
2 min read
On this page

How to set state to false after 10 seconds in React Native#

If you are working with React Native and you want to set state to false after a specific time, you can easily achieve it using the setTimeout() function. Here's how you can do it:

Method 1: Using setTimeout()#

Text
import React, { useState, useEffect } from 'react';
import { Text, View } from 'react-native';

const App = () => {
  const [showText, setShowText] = useState(true);
  
  useEffect(() => {
    setTimeout(() => {
      setShowText(false);
    }, 10000); // set time in milliseconds
  }, []);

  return (
    <View>
      {showText ? <Text>Hello World!</Text> : null}
    </View>
  );
};

export default App;
  • useState() hook is used to define a state variable showText.
  • useEffect() hook is used to call the setTimeout() function.
  • The setTimeout() function is used to set the state of showText to false after 10 seconds (10000 milliseconds).
  • <Text> component is rendered conditionally based on the value of showText.

This method is simple and easy to understand. However, if you have multiple components that depend on the same state variable, you may need to pass the state variable as a prop to each component which can be tedious.

Method 2: Using Context API#

Text
// App.js
import React, { useState } from 'react';
import { Text, View } from 'react-native';

export const AppContext = React.createContext();

const App = () => {
  const [showText, setShowText] = useState(true);

  const value = { showText, setShowText };

  return (
    <AppContext.Provider value={value}>
      <View>
        <Text>Hello World!</Text>
      </View>
    </AppContext.Provider>
  );
};

export default App;

// ChildComponent.js
import React, { useContext, useEffect } from 'react';
import { Text, View } from 'react-native';
import { AppContext } from './App';

const ChildComponent = () => {
  const { showText, setShowText } = useContext(AppContext);

  useEffect(() => {
    setTimeout(() => {
      setShowText(false);
    }, 10000); // set time in milliseconds
  }, []);

  return (
    <View>
      {showText ? <Text>Hello World!</Text> : null}
    </View>
  );
};

export default ChildComponent;
  • useState() hook is used to define a state variable showText.
  • AppContext is created using the React.createContext() function.
  • The state variable showText and the function to update it setShowText are passed as values to the context provider.
  • ChildComponent component uses the useContext() hook to get the state variable showText and the function to update it setShowText.
  • The setTimeout() function is used to set the state of showText to false after 10 seconds (10000 milliseconds).
  • <Text> component is rendered conditionally based on the value of showText.

This method is useful when you have multiple components that depend on the same state variable. You can avoid prop drilling by using the Context API.

Choose the method that suits your needs and implement it accordingly. Happy coding!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC