useState
is a Hook that lets you add state to functional components. It returns an array with the current state and a function to update it.
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
};
import { useState } from "react";
function App() {
return (
<div style={{background: "#dfe6e9", height: "100vh" }}>
<ToggleMessage />
<ToggleMessage />
<ToggleMessage />
</div>
)
}
// the component isnt re-rendering
// because we havent used a state variable
const ToggleMessage = () => {
let [notificationCount, setNotificationCount] = useState(0);
console.log("re-render");
function increment() {
setNotificationCount(notificationCount + 1);
}
return (
<div>
<button onClick={increment}>
Increase count
</button>
{notificationCount}
</div>
);
};
const style = { width: 200, backgroundColor: "white", borderRadius: 10, borderColor: "gray", borderWidth: 1, padding: 20 }
export function PostComponent({name, subtitle, time, image, description}) {
return <div style={style}>
<div style={{display: "flex"}}>
<img src={image} style={{
width: 30,
height: 30,
borderRadius: 20
}} />
<div style={{fontSize: 10, marginLeft: 10}}>
<b>
{name}
</b>
<div>{subtitle}</div>
{(time !== undefined) ? <div style={{display: 'flex'}}>
<div>{time}</div>
<img src={"<https://media.istockphoto.com/id/931336618/vector/clock-vector-icon-isolated.jpg?s=612x612&w=0&k=20&c=I8EBJl8i6olqcrhAtKko74ydFEVbfCQ6s5Pbsx6vfas=>"} style={{width: 12, height: 12}} />
</div> : null}
</div>
</div>
<div style={{fontSize: 12}}>
{description}
</div>
</div>
}