Skip to content
GitHub

Getting Started with React

This guide will help you get started with Effectify in your React application. We’ll walk through setting up the basic dependencies and creating your first Effect-powered React component.

Before you begin, make sure you have:

  • Node.js 18 or later
  • A React application (Create React App, Vite, Next.js, etc.)
  • Basic knowledge of React and TypeScript

Choose the packages you need for your project:

For data fetching with TanStack Query and Effect:

npm install @effectify/react-query @tanstack/react-query effect

For pre-built UI components:

npm install @effectify/react-ui

For real-time chat functionality:

npm install @effectify/chat-react

First, set up TanStack Query in your app root:

// App.tsx
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"

const queryClient = new QueryClient()

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <YourAppComponents />
      <ReactQueryDevtools initialIsOpen={false} />
    </QueryClientProvider>
  )
}

Create a simple Effect that fetches data:

// effects/user.ts
import { Effect } from "effect"

export interface User {
  id: number
  name: string
  email: string
}

export const fetchUser = (id: number) =>
  Effect.tryPromise({
    try: () => fetch(`/api/users/${id}`).then((res) => res.json()),
    catch: (error) => new Error(`Failed to fetch user: ${error}`),
  })

Now use the Effect in a React component:

// components/UserProfile.tsx
import { useQuery } from "@tanstack/react-query"
import { Effect } from "effect"
import { fetchUser } from "../effects/user"

interface UserProfileProps {
  userId: number
}

export function UserProfile({ userId }: UserProfileProps) {
  const {
    data: user,
    isLoading,
    error,
  } = useQuery({
    queryKey: ["user", userId],
    queryFn: () => Effect.runPromise(fetchUser(userId)),
  })

  if (isLoading) return <div>Loading...</div>
  if (error) return <div>Error: {error.message}</div>
  if (!user) return <div>User not found</div>

  return (
    <div>
      <h1>{user.name}</h1>
      <p>{user.email}</p>
    </div>
  )
}

Now that you have the basics set up, explore the specific packages:

Effect provides excellent error handling capabilities:

const fetchUserWithRetry = (id: number) =>
  fetchUser(id).pipe(
    Effect.retry({ times: 3 }),
    Effect.catchAll((error) => Effect.succeed({ id, name: "Unknown", email: "unknown@example.com" })),
  )

You can compose multiple Effects together:

const fetchUserWithPosts = (id: number) =>
  Effect.all([fetchUser(id), fetchUserPosts(id)]).pipe(
    Effect.map(([user, posts]) => ({ user, posts })),
  )
  1. Effect not running: Make sure to call Effect.runPromise() in your query function
  2. Type errors: Ensure you have the latest versions of Effect and TypeScript
  3. Build errors: Check that all peer dependencies are installed

If you run into issues: