You are on page 1of 1

STATE

It is like a variable which holds data & we can update it with time and urgency.
Remember:
We can’t use a state outside the component.
Use:
First of all import a hook from react with useState named. Like:
import React, {useState} from 'react';
Now declare an array variable which is equal to the hook.
const [value, updateValue] = useState();
Here the initial value of the state is empty because we have not set a value. Now let's give a value. E.g.
const name = "ALLAH";
const [value, updateValue] = useState(name);
Output:
export default function App() {
const name = "ALLAH"; ← ← ← ← ← The initial value is ALLAH.
const [value, updateValue] = useState(name);
return (
<View style={styles.container}>
<Text>{value}</Text> ← ← ← ← ← This is, how to use state variable.
<StatusBar style="auto" />
</View>
);
}
Now update the state value:
export default function App() {
const name = "ALLAH";
const [value, updateValue] = useState(name);
const CheckState = () =>{
updateValue("ALLAH ho Akbar") ← ← ← ← Here we update the value of our state.
};
return (
<View style={styles.container}>
<Text>{value}</Text>
<Button title='Update Value' onPress={CheckState} />
<StatusBar style="auto" />
</View>
);
}

You might also like