What is State ?
It is information/data holder in react component. State manage data.
There are many points regarding states let see one by one.
1) States are mutables means in future data can be updated in states but side by side the props are not immutable.thay can;t update.
2 )When we update variable in component, the component don't get updated.
3 )When state update, component re-render than we can see the new changes.
4) We can't use state outside of component.
Now question is how to use state in component ?
In React we use useState hook to track/maintain states in components.
To use state first we import useState hook from React.
Visst the link for state example laraveldevdiary
State example code.
import { useState } from "react";
import { Button, Text, View } from "react-native";
const State = () => {
const [name,setName] = useState('Adnan');
function handleState() {
setName('Ammal Adnan');
}
return (
<View>
<Text style={{fontSize: 20}}>State in react-native!</Text>
<Text>My Name : {name}</Text>
<Text>onPress update state</Text>
<Button title="update state" color={'green'} onPress={handleState} />
</View>
);
}
export default State;
Comments
Post a Comment