Developer Code Snippets

Explore helpful, reusable code snippets for React, Next.js, and modern JavaScript.

React useState Example

A simple example of using the useState hook in React to manage component state dynamically.

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

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

export default Counter;

Fetch Data in Next.js

Fetching data from an API using getServerSideProps in Next.js for server-side rendering.

export async function getServerSideProps() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();

  return { props: { data } };
}