The state is mutable while props are immutable. This means that state can be updated in the future while props cannot be updated.
There are two types of data that control a component:
There are two types of data that control a component:
props and state. props are set by the parent and they are fixed throughout the lifetime of a component. For data that is going to change, we have to use state
Using State
This is our root component. We are just importing Home which will be used in most of the chapters
import { AppRegistry, Text, View } from 'react-native';
class Blink extends Component {
constructor(props) {
super(props);
this.state = {isShowingText: true};
// Toggle the state every second
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 1000);
}
render() {
let display = this.state.isShowingText ? this.props.text : ' ';
return (
<Text>{display}</Text>
);
}
}
export default class BlinkApp extends Component {
render() {
return (
<View>
<Blink text='I love to blink' />
<Blink text='Yes blinking is so great' />
<Blink text='Why did they ever take this out of HTML' />
<Blink text='Look at me look at me look at me' />
</View>
);
}
}
// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => BlinkApp);
No comments:
Post a Comment