Last active
June 5, 2019 15:31
-
-
Save RoRoGadget/3fcfb9621ad7066a42c3ca5823de2da4 to your computer and use it in GitHub Desktop.
SwiftUI4RN-State
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { Component } from 'react'; | |
| import { AppRegistry, Text, View } from 'react-native'; | |
| class Blink extends Component { | |
| componentDidMount(){ | |
| // Toggle the state every second | |
| setInterval(() => ( | |
| this.setState(previousState => ( | |
| { isShowingText: !previousState.isShowingText } | |
| )) | |
| ), 1000); | |
| } | |
| //state object | |
| state = { isShowingText: true }; | |
| render() { | |
| if (!this.state.isShowingText) { | |
| return null; | |
| } | |
| return ( | |
| <Text>{this.props.text}</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> | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import SwiftUI | |
| import PlaygroundSupport | |
| // https://facebook.github.io/react-native/docs/state | |
| struct Blink: View { | |
| @State var isShowingText: Bool = true | |
| var body: some View { | |
| Group { | |
| Spacer() | |
| Toggle(isOn: $isShowingText) { | |
| Text("Show Text") | |
| } | |
| Spacer() | |
| (self.isShowingText) | |
| ? Text("I love to toggle") | |
| : nil | |
| (self.isShowingText) | |
| ? Text("Yes blinking is so great") | |
| : nil | |
| (self.isShowingText) | |
| ? Text("Why did they ever take this out of Swift") | |
| : nil | |
| (self.isShowingText) | |
| ? Text("Look at me look at me look at me") | |
| : nil | |
| Spacer() | |
| } | |
| } | |
| } | |
| PlaygroundPage.current.liveView = UIHostingController(rootView: Blink()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment