javascript
<template>
<div>{{ displayString }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
displayString: String
});
</script>
<template>
<div>{{ displayString }}</div>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
displayString: String
});
</script>
function MyComponent(props) {
return <div>{props.displayString}</div>;
}
<template>
<p v-if="condition">Condition is true</p>
<p v-else>Condition is false</p>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
condition: Boolean
});
</script>
function ConditionalComponent({ condition }) {
return (
<>
{condition ? (
<p>Condition is true</p>
) : (
<p>Condition is false</p>
)}
</>
);
}
// Usage
<ConditionalComponent condition={true} />;
<template>
<intermediate-component :data="data" />
</template>
<script setup>
import { defineProps } from 'vue';
import IntermediateComponent from './IntermediateComponent.vue';
const props = defineProps({
data: String
});
</script>
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" />;
<template>
<button @click="setClicked">
{{ clicked ? "Button clicked" : "Click me" }}
</button>
</template>
<script setup>
import { ref } from 'vue';
const clicked = ref(false);
function setClicked() {
clicked.value = true;
}
</script>
import { useState } from "react";
function ClickableComponent() {
const [clicked, setClicked] = useState(false);
return (
<button onClick={() => setClicked(true)}>
{clicked ? "Button clicked" : "Click me"}
</button>
);
}
<template>
<input
type="text"
v-model="text"
placeholder="Enter text"
/>
</template>
<script setup>
import { ref } from 'vue';
const text = ref('');
</script>
function TextInputComponent() {
const [text, setText] = useState("");
return (
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Enter text"
/>
);
}
Vue.js 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.
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.
<template>
<ul>
<li v-for="item in items" :key="item">
{{ item }}
</li>
</ul>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
items: Array
});
</script>
<!-- Usage -->
<list-component :items="['Item 1', 'Item 2', 'Item 3']"></list-component>
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} />;
<template>
<ul>
<li v-for="person in items" :key="person.id">
Name: {{ person.name }}, Age: {{ person.age }}
</li>
</ul>
</template>
<script setup>
import { defineProps } from 'vue';
const props = defineProps({
items: Array
});
</script>
<!-- Usage -->
<item-keys-example
:items="[
{ name: 'John', age: 30, id: '1' },
{ name: 'Jane', age: 28, id: '2' },
{ name: 'Bob', age: 25, id: '3' }
]"
></item-keys-example>
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" },
]}
/>;
// ParentComponent.vue
<template>
<div>
<slot name="header"></slot>
<slot name="content"></slot>
</div>
</template>
// ChildComponent.vue
<template>
<p>Child Content</p>
</template>
// Usage
<parent-component>
<template v-slot:header>
<h1>Header</h1>
</template>
<template v-slot:content>
<child />
</template>
</parent-component>
function Parent({ header, content }) {
return (
<div>
{header}
{content}
</div>
);
}
// Usage
<Parent header={<h1>Header</h1>} content={<Child />} />;
function Child() {
return <p>Child Content</p>;
}
Vue.js doesn't have a direct analog to modifiers in Jetpack Compose or SwiftUI. Instead, you can use inline styles or CSS classes.
<template>
<div :style="style">Hello, World!</div>
</template>
<script setup>
import { reactive } from 'vue';
const style = reactive({
padding: '16px',
backgroundColor: 'blue',
color: 'white'
});
</script>
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>;
}
<template>
<button @click="incrementCount">
Count: {{ count }}
</button>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
function incrementCount() {
count.value++;
}
</script>
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
<!-- ParentComponent.vue -->
<template>
<intermediate />
</template>
<script setup>
import { provide, ref } from 'vue';
import Intermediate from './IntermediateComponent.vue';
const data = ref('Some data');
provide('dataKey', data);
</script>
<!-- IntermediateComponent.vue -->
<template>
<child />
</template>
<script setup>
import Child from './ChildComponent.vue';
</script>
<!-- ChildComponent.vue -->
<template>
<p>Received data: {{ data }}</p>
</template>
<script setup>
import { inject } from 'vue';
const data = inject('dataKey');
</script>
<!-- Usage -->
<parent-component data="Some data"></parent-component>
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" />;
<template>
<div></div>
</template>
<script setup>
import { onMounted } from 'vue';
onMounted(() => {
// Perform side effect here
});
</script>
import { useEffect } from "react";
function SideEffectOnLoadComponent() {
useEffect(() => {
// Perform side effect, e.g. fetch data, update external data source
}, []);
// Other UI components
return <div />;
}