Coder Social home page Coder Social logo

Comments (5)

kiliman avatar kiliman commented on April 25, 2024 1

It's always difficult to keep a tutorial updated with the latest changes (especially on a fast-paced project like Remix).

The "current" recommended approach to typing loaders is as follows:

  • use LoaderArgs instead of LoaderFunction
  • always wrap return value in json helper
  • use useLoaderArgs<typeof loader>() instead of as LoaderData
import { json, type LoaderArgs } from '@remix-run/node'
import { db } from '~/db.server'

export const loader = async ({request, params}: LoaderArgs => {
  const { jokeId } = params
  const joke = await db.joke.findUnique({
    where: { id: jokeId },
  })
  return json({joke})
}

export default function Joke() {
  const { joke } = useLoaderData<typeof loader>()
  return <div>{ joke.name }</div>
}

NOTE: Since Remix always returns loader data as serialized JSON, values like Date will be converted to a string. So the inferred type will by string not Date. If you would like to maintain the native types, then you can use remix-typedjson which will maintain the native types across the entire request.

https://github.com/kiliman/remix-typedjson

from examples.

MichaelDeBoey avatar MichaelDeBoey commented on April 25, 2024

@PythonCreator27 I'm currently working on a PR that will update these old usages to the newly recommended one

from examples.

franklinjavier avatar franklinjavier commented on April 25, 2024

It would be great if has a codemod to update all the examples

from examples.

MichaelDeBoey avatar MichaelDeBoey commented on April 25, 2024

@franklinjavier I thought about creating a codemod for this, but since it has so many edge-cases that aren't straight-forward, it's better to do it manually yourself in each codebase

from examples.

franklinjavier avatar franklinjavier commented on April 25, 2024

@franklinjavier I thought about creating a codemod for this, but since it has so many edge-cases that aren't straight-forward, it's better to do it manually yourself in each codebase

agree

from examples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.