Comments (5)
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 ofLoaderFunction
- always wrap return value in
json
helper - use
useLoaderArgs<typeof loader>()
instead ofas 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 bystring
notDate
. If you would like to maintain the native types, then you can useremix-typedjson
which will maintain the native types across the entire request.
https://github.com/kiliman/remix-typedjson
from examples.
@PythonCreator27 I'm currently working on a PR that will update these old usages to the newly recommended one
from examples.
It would be great if has a codemod to update all the examples
from examples.
@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 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)
- Infinite scrolling example seems to be broken in codesandbox
- Emotion example is outdated HOT 2
- Cookie but missing user in db, ends up being a promise and not redirecting to login HOT 2
- [Feature Request] Framework user wants to have an example integration with Cloudscape Design System HOT 1
- _official-realtime-app Concurrent connections HOT 1
- tailwind styles are not processed in v1.19.2 but are in v1.19.1 HOT 1
- chakra-ui example has hydration issues HOT 2
- Stitches example: Greedy regexp pattern potentially breaks markup HOT 1
- MSW example not working HOT 1
- mdx-bundler example
- Example for Vite + Styled Components?? HOT 1
- Vite + Styled Components Example HOT 4
- Create an example of an app with nested routes
- nprogress uses useTransition, should it use useNavigation instead? HOT 2
- Feature: Pocketbase Auth Example with Realtime Data
- Stripe HOT 1
- sharing-loader-data example choice justification
- Transforming the `socket` example using `vite`
- [framer-route-animation] window should not scroll to top immediately
- useLoaderData typing not working in contacts tutorial
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.