From Svelte to React
posted on: 7/3/2023, 8:42:50 PM
last updated: 7/4/2023, 9:13:52 PM
Reading Time: less than a minute read
NOTE: This entry is in a preview state. It may not be complete yet,
Contents
- Creating a Web App Skaffold
- Defining a Component
- Nesting Components
- Returning Multiple Components
- Styling Components
- Displaying Variables
- Conditional Rendering
- Rendering Data Structures
- Passing Props to Components
- Event Raising and Handling
- Handling Component State
- Using Framework-Specific Features
- Render Caching
- Sharing State Between Components
- Fallbacks While Loading Data
- Server-Side Components
Creating a Web App Skaffold
pnpm create vite
# follow the prompts!
pnpm create svelte@latest my-app
cd my-app
pnpm install
pnpm dev
pnpx create-next-app
Defining a Component
<div class="my-component">
Hello World!
</div>
function MyComponent() {
return (
<div class="my-component">
Hello World!
</div>
)
}
Nesting Components
/* myComponent.js */
<div class="my-component">
Hello World!
</div>
/* app.js */
<script>
import MyComponent from './myComponent';
</script>
<div class="app">
This is an app!
<MyComponent/>
</div>
/* myComponent.jsx */
export default function MyComponent() {
return (
<div class="my-component">
Hello World!
</div>
)
}
/* app.jsx */
import MyComponent from './myComponent.js';
export default function App() {
return (
<div class="app">
This is an app!
<MyComponent/>
</div>
)
}
Returning Multiple Components
<p>
Hello World!
</p>
<p>
Nice to meet you!
</p>
JSX requires a return of only one element, so you need to nest multiple elements inside a single element,
such as the <React.Fragment>...</React.Fragment>
element or the null-element (<>...</>
)
function MyComponent() {
return (
<>
<p>
Hello World!
</p>
<p>
Nice to meet you!
</p>
</>
)
}
Styling Components
<script>
let borderColor = 'blue';
</script>
<p class="red-text" style:borderColor style="background-color: black; border-color: {borderColor}">
Hello World!
</p>
<style>
p.red-text {color: red;}</style>
You can't use class
in JSX, as it is a reserved word in ECMAScript. Use className
instead. If you want
to use inline styles, you need to use the JS properties of the style
object instead of the html/css properties
used in normal HTML. the style
attribute takes an object, so you can pass an object
function MyComponent() {
let borderColor = 'blue';
const styles={ borderColor }
return (
<p className="red-text" style="{{...styles, background-color: 'black' }}">
Hello World!
</p>
)
}
Displaying Variables
Conditional Rendering
Rendering Data Structures
Passing Props to Components
Event Raising and Handling
Handling Component State
Using Framework-Specific Features
use*
function in react.
https://github.com/joshnuss/react-hooks-in-svelte
Render Caching
useCallback
in react. Completely not necessary in svelte. use stores to save state.
Sharing State Between Components
Fallbacks While Loading Data
React's <Suspense>
element
Server-Side Components
use client/server
in react. +server.js
in sveltekit.