jsx
function MyComponent(props) {
return <div>{props.displayString}</div>;
}
function MyComponent(props) {
return <div>{props.displayString}</div>;
}
struct MyComponent: View {
var displayString: String
var body: some View {
Text(displayString)
}
}
@Composable
fun MyComponent(
displayString: String
) {
Text(displayString)
}
function ConditionalComponent({ condition }) {
return (
<>
{condition ? (
<p>Condition is true</p>
) : (
<p>Condition is false</p>
)}
</>
);
}
// Usage
<ConditionalComponent condition={true} />;
struct ConditionalComponent: View {
let condition: Bool
var body: some View {
Group {
if condition {
Text("Condition is true")
} else {
Text("Condition is false")
}
}
}
}
// Usage
ConditionalComponent(condition: true)
@Composable
fun ConditionalComponent(condition: Boolean) {
if (condition) {
Text("Condition is true")
} else {
Text("Condition is false")
}
}
// Usage
ConditionalComponent(condition = true)
function Parent({ data }) {
return <IntermediateComponent data={data} />;
}
function IntermediateComponent({ data }) {
return <ChildComponent data={data} />;
}
function ChildComponent({ data }) {
return <p>Received data: {data}</p>;
}
// Usage
<Parent data="Some data" />;
struct Parent: View {
let data: String
var body: some View {
IntermediateComponent(data: data)
}
}
struct IntermediateComponent: View {
let data: String
var body: some View {
ChildComponent(data: data)
}
}
struct ChildComponent: View {
let data: String
var body: some View {
Text("Received data: \(data)")
}
}
// Usage
Parent(data: "Some data")
@Composable
fun Parent(data: String) {
IntermediateComponent(data = data)
}
@Composable
fun IntermediateComponent(data: String) {
ChildComponent(data = data)
}
@Composable
fun ChildComponent(data: String) {
Text("Received data: $data")
}
// Usage
Parent(data = "Some data")
import { useState } from "react";
function ClickableComponent() {
const [clicked, setClicked] = useState(false);
return (
<button onClick={() => setClicked(true)}>
{clicked ? "Button clicked" : "Click me"}
</button>
);
}
struct ClickableComponent: View {
@State private var clicked = false
var body: some View {
Button(action: {
clicked = true
}) {
Text(clicked ? "Button clicked" : "Click me")
}
}
}
@Composable
fun ClickableComponent() {
var clicked by remember { mutableStateOf(false) }
Button(onClick = { clicked = true }) {
Text(if (clicked) "Button clicked" else "Click me")
}
}
function TextInputComponent() {
const [text, setText] = useState("");
return (
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Enter text"
/>
);
}
struct TextInputComponent: View {
@State private var text = ""
var body: some View {
TextField("Enter text", text: $text)
}
}
@Composable
fun TextInputComponent() {
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { newText -> text = newText },
label = { Text("Enter text") }
)
}
React doesn't have a built-in preview feature. However, you can use a tool like Storybook to create previews for your components in a separate development environment.
struct ExampleComponent: View {
var body: some View {
Text("Hello, World!")
}
}
struct ExampleComponent_Previews: PreviewProvider {
static var previews: some View {
ExampleComponent()
}
}
@Composable
fun ExampleComponent() {
Text("Hello, World!")
}
@Preview(showBackground = true)
@Composable
fun ExampleComponentPreview() {
ExampleComponent()
}
Additionally, you can also use Showkase, an open source library by Airbnb that allows you to view themes preview functions in an auto-generated component browser that can be viewed on an Android device.
function ListComponent({ items }) {
return (
<ul>
{items.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
);
}
// Usage
const items = ["Item 1", "Item 2", "Item 3"];
<ListComponent items={items} />;
struct ListComponent: View {
let items: [String]
var body: some View {
List(items, id: \.self) { item in
Text(item)
}
}
}
// Usage
let items = ["Item 1", "Item 2", "Item 3"]
ListComponent(items: items)
@Composable
fun ListComponent(items: List<String>) {
LazyColumn {
items(items) { item ->
Text(item)
}
}
}
// Usage
val items = listOf("Item 1", "Item 2", "Item 3")
ListComponent(items = items)
function ItemKeysExample({ items }) {
return (
<ul>
{items.map((person) => (
<li key={person.id}>
Name: {person.name}, Age: {person.age}
</li>
))}
</ul>
);
}
// Usage
<ItemKeysExample
items={[
{ name: "John", age: 30, id: "1" },
{ name: "Jane", age: 28, id: "2" },
{ name: "Bob", age: 25, id: "3" },
]}
/>;
struct Person: Identifiable {
let name: String
let age: Int
let id: String
}
struct ItemKeysExample: View {
let items: [Person]
var body: some View {
List(items) { person in
Text("Name: \(person.name), Age: \(person.age)")
}
}
}
// Usage
ItemKeysExample(items: [Person(name: "John", age: 30, id: "1"), Person(name: "Jane", age: 28, id: "2"), Person(name: "Bob", age: 25, id: "3")])
data class Person(val name: String, val age: Int, val id: String)
@Composable
fun ItemKeysExample(items: List<Person>) {
LazyColumn {
items(items, key = { person -> person.id }) { person ->
Text("Name: ${person.name}, Age: ${person.age}")
}
}
}
function Parent({ header, content }) {
return (
<div>
{header}
{content}
</div>
);
}
// Usage
<Parent header={<h1>Header</h1>} content={<Child />} />;
function Child() {
return <p>Child Content</p>;
}
struct Parent<Header: View, Content: View>: View {
let header: Header
let content: Content
var body: some View {
VStack {
header
content
}
}
}
// Usage
Parent(
header: Text("Header"),
content: Child()
)
struct Child: View {
var body: some View {
Text("Child Content")
}
}
@Composable
fun Parent(
header: @Composable () -> Unit,
content: @Composable () -> Unit
) {
Column {
header()
content()
}
}
// Usage
Parent(
header = { Text("Header") },
content = { Child() }
)
@Composable
fun Child() {
Text("Child Content")
}
React doesn't have a direct analog to modifiers in Jetpack Compose or SwiftUI. Instead, you can use inline styles or CSS classes.
function ModifiersExample() {
const style = {
padding: "16px",
backgroundColor: "blue",
color: "white",
};
return <div style={style}>Hello, World!</div>;
}
struct ModifiersExample: View {
var body: some View {
Text("Hello, World!")
.padding(EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16))
.background(Color.blue)
}
}
@Composable
fun ModifiersExample() {
Text(
"Hello, World!",
modifier = Modifier
.padding(16.dp)
.background(Color.Blue)
)
}
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
struct Counter: View {
@State private var count = 0
var body: some View {
Button(action: {
count += 1
}) {
Text("Count: \(count)")
}
}
}
@Composable
fun Counter() {
var count by remember { mutableStateOf(0) }
Button(onClick = { count = count + 1 }) {
Text("Count: $count")
}
}
import { createContext, useContext } from "react";
const CustomContext = createContext();
function Parent({ data }) {
return (
<CustomContext.Provider value={data}>
<Intermediate />
</CustomContext.Provider>
);
}
function Intermediate() {
return <Child />;
}
function Child() {
const data = useContext(CustomContext);
return <p>Received data: {data}</p>;
}
// Usage
<Parent data="Some data" />;
struct CustomEnvironmentKey: EnvironmentKey {
static let defaultValue: String = ""
}
extension EnvironmentValues {
var customData: String {
get { self[CustomEnvironmentKey.self] }
set { self[CustomEnvironmentKey.self] = newValue }
}
}
struct Parent: View {
let data: String
var body: some View {
Intermediate().environment(\.customData, data)
}
}
struct Intermediate: View {
var body: some View {
Child()
}
}
struct Child: View {
@Environment(\.customData) private var data
var body: some View {
Text("Received data: \(data)")
}
}
// Usage
Parent(data: "Some data")
val CustomLocal = compositionLocalOf<String> { "Default data" }
@Composable
fun Parent(data: String) {
CompositionLocalProvider(CustomLocal provides data) {
Intermediate()
}
}
@Composable
fun Intermediate() {
Child()
}
@Composable
fun Child() {
val data = CustomLocal.current
Text("Received data: $data")
}
// Usage
Parent(data = "Some data")
import { useEffect } from "react";
function SideEffectOnLoadComponent() {
useEffect(() => {
// Perform side effect, e.g. fetch data, update external data source
}, []);
// Other UI components
return <div />;
}
struct SideEffectOnLoadComponent: View {
@State private var hasPerformedSideEffect = false
var body: some View {
if !hasPerformedSideEffect {
DispatchQueue.main.async {
// Perform side effect, e.g. fetch data, update external data source
hasPerformedSideEffect = true
}
}
// Other UI components
Text("Hello, World!")
}
}
@Composable
fun SideEffectOnLoadComponent() {
LaunchedEffect(Unit) {
// Perform side effect, e.g. fetch data, update external data source
}
// Other UI components
Text("Hello, World!")
}