React Native

What is the purpose of the useRef hook in React Native?

Medium
7
Added
The useRef hook is used to create a mutable ref object whose current property is initialized to the passed argument. It can be used to access DOM elements or to store any mutable value.

Solution Code

React Native
-- Example Code --
import React, { useRef } from "react";
import { View, Text, Button } from "react-native";

const App = () => {
  const countRef = useRef(0);

  const increment = () => {
    countRef.current += 1;
    console.log(countRef.current);
  };

  return (
    <View>
      <Text>Count: {countRef.current}</Text>
      <Button title="Increment" onPress={increment} />
    </View>
  );
};

export default App;
Explanation
This code demonstrates how to use the useRef hook to store a mutable value.

Guided Hints

{'hint': 'Use useRef to create a mutable ref object.'}
{'hint': 'It can be used to access DOM elements.'}