Skip to main content

Props

REACT​

What are React Props?​

Props (short for properties) are used to pass data from one component (parent) to another (child). They are read-only, meaning the child component cannot change them.

Think of props as function arguments, but for components.


Props and State​

Definition​

  • Props are used to pass data into a component.
  • State is used to manage internal data within a component.

Syntax​

type MyProps = {
title: string;
};

const MyComponent: React.FC<MyProps> = ({ title }) => {
return <h2>{title}</h2>;
};

Example​

import React, { useState } from 'react';

type MyProps = {
name: string;
};

const Greeting: React.FC<MyProps> = ({ name }) => {
const [count, setCount] = useState<number>(0); // state

return (
<div>
<h2>Hello, {name}!</h2> {/* Using props */}
<p>You clicked {count} times.</p>
<button onClick={() => setCount(count + 1)}>Click Me</button> {/* Updating state */}
</div>
);
};

export default Greeting;

Passing Data with Props​

You can pass values to a child component as attributes.

Syntax of passing data with props​

<ChildComponent title="Welcome" />

Example of passing data with props​

// ParentComponent.tsx
import React from 'react';
import Child from './Child';

const Parent: React.FC = () => {
return <Child title="React Props Example" />;
};

// Child.tsx
type Props = {
title: string;
};

const Child: React.FC<Props> = ({ title }) => {
return <h1>{title}</h1>; // Displaying prop from parent
};

export default Parent;

Managing Local Component State​

State is local data in a component that can change over time (like user input, count, etc.)

Syntax for managing local component state​

const [value, setValue] = useState<type>(initialValue);

Example for managing local component state​

import React, { useState } from 'react';

const Counter: React.FC = () => {
const [count, setCount] = useState<number>(0); // Local state

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Add +1</button>
</div>
);
};

export default Counter;

useState Hook (Functional Components)​

The useState hook lets functional components hold and update internal state.

Syntax of useState Hook​

const [state, setState] = useState<type>(initialValue);

Example of useState Hook​

import React, { useState } from 'react';

const NameInput: React.FC = () => {
const [name, setName] = useState<string>("");

return (
<div>
<input
value={name}
onChange={(e) => setName(e.target.value)} // update state on input
placeholder="Enter your name"
/>
<p>Your name is: {name}</p>
</div>
);
};

export default NameInput;

Summary​

ConceptPurpose
PropsPass data into components
StateManage dynamic/local data
Passing with PropsUse props to configure child behavior
useStateReact Hook to manage state in functions

SVELTE​

Props & State in Svelte​

What are Props?​

Definition of Svelte Props​

In Svelte, props are component parameters — values passed from a parent to a child component.

Like function arguments for components. Props are read-only in the child component.


Props and State (Reactivity)​

Props → passed from parent​

State → declared as variables and are reactive by default in Svelte​


Syntax (Props)​

<!-- ChildComponent.svelte -->
<script lang="ts">
export let title: string; // Declare props using `export let`
</script>

<h2>{title}</h2>

Example(Props + State)​

<!-- Greeting.svelte -->
<script lang="ts">
export let name: string; // prop
let count: number = 0; // state (reactive)
</script>

<div>
<h2>Hello, {name}!</h2>
<p>You clicked {count} times.</p>
<button on:click={() => count += 1}>Click Me</button>
</div>

Passing Data with Props in Svelte​

Syntax (Passing to Child)​

<!-- ParentComponent.svelte -->
<script lang="ts">
import Child from './ChildComponent.svelte';
</script>

<Child title="Svelte Props Example" />
<!-- ChildComponent.svelte -->
<script lang="ts">
export let title: string;
</script>

<h1>{title}</h1>

Managing Local Component State in Svelte​

In Svelte, any variable declared in <script> is reactive by default and acts as state.


Syntax for managing local component state in svelte​

<script lang="ts">
let count: number = 0;
</script>

<p>{count}</p>
<button on:click={() => count++}>Add +1</button>

Example of managing local componenet state in svelte​

<!-- Counter.svelte -->
<script lang="ts">
let count: number = 0;
</script>

<div>
<p>Count: {count}</p>
<button on:click={() => count++}>Add +1</button>
</div>

Local State (Input Handling)​

Syntax of local state input handling​

<script lang="ts">
let variableName: string = "";
</script>

<input bind:value={variableName} />
<p>{variableName}</p>

Use bind:value={...} for two-way data binding in Svelte.


Example of local state input handling​

<!-- NameInput.svelte -->
<script lang="ts">
let name: string = "";
</script>

<input bind:value={name} placeholder="Enter your name" />
<p>Your name is: {name}</p>

bind:value={name} keeps the input field and name variable in sync automatically.

React vs Svelte (Hooks vs Reactivity)​

ConceptReactSvelte
Local stateuseState() hookReactive variables (let count = 0)
Side effectsuseEffect() hookonMount(), reactive statements
Derived/computed dataCustom hooks or logic$: reactive statements
Input bindingvalue, onChange with statebind:value={name}

Summary of props in React and Svelte​

ConceptReactSvelte
PropsPassed using propsDeclared with export let
StateuseState for local stateReactive variables (let)
Update StatesetState(value)Direct assignment (count++)
Input HandlingonChange, setStatebind:value={name}
Pass Props<Comp prop="value" />Same: <Child title="Hello" />
Use Props in ChildDestructure in function parametersexport let title;