Skip to content

Instantly share code, notes, and snippets.

@RoRoGadget
Last active June 5, 2019 15:31
Show Gist options
  • Select an option

  • Save RoRoGadget/3fcfb9621ad7066a42c3ca5823de2da4 to your computer and use it in GitHub Desktop.

Select an option

Save RoRoGadget/3fcfb9621ad7066a42c3ca5823de2da4 to your computer and use it in GitHub Desktop.
SwiftUI4RN-State
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>
);
}
}
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