58
loading...
This website collects cookies to deliver better user experience
npx create-remix@latest
npm install
as we will be using Yarn today.cd run-rsp
code .
yarn
yarn dev
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration
} from "remix";
export function meta() {
return { title: "New Remix App" };
}
export default function App() {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
</html>
);
}
<Outlet />
component. We'll edit that in a second.meta()
function to whatever you want. Save the file and notice that the title of your app in the browser changes almost instantly. (Check the browser tab of the app or right-click and view page source)routes\index.jsx
. Open the file up and delete everything. Save the file. You'll now see an error in the browser.export default function Index() {
return (
<div>
<h1>I'll soon be connected to SQLite</h1>
</div>
);
}
root.jsx
file, add a links()
function. We are already importing the Links
component and using it in the head of our document, so whatever we export from our links()
function will be used in the Links
component.// root.jsx
export function links() {
return [
{
rel: "stylesheet",
href: "https://unpkg.com/[email protected]/dist/reset.min.css"
}
];
}
styles
folder in app
and add a file called global.css
with a rule or two.root.jsx
. Import your styles and then add another link to the array in links()
.import globalStyles from '~/styles/global.css'
...
// root.jsx
export function links() {
return [
{
rel: "stylesheet",
href: "https://unpkg.com/[email protected]/dist/reset.min.css"
},
{
rel: "stylesheet",
href: globalStyles
}
];
}
yarn add @prisma/client
yarn add -D prisma
npx prisma init --datasource-provider sqlite
schema.prisma
file for Soccer Teams. Install the Prisma extension if you haven't already to format your Prisma files. Prettier may try to override Prisma's formatting, so if that's the case, right-click the file, select format document, and indicate that Prisma should format the file. Now you don't need to worry about getting your models formatted. We are going to keep things simple here just to show how everything is wired up together. Our database will be a list of soccer teams, or whatever kind of teams you like.schema.prisma
file.generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model Team {
id String @id @default(uuid())
team String
country String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
npx prisma db push
npx prisma studio
app/lib/db.server.js
) to configure our connections to the database. Note that the "server" in the file tells Remix to run this on the server.import { PrismaClient } from "@prisma/client";
let prisma;
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient();
} else {
if (!global.prisma) {
global.prisma = new PrismaClient();
}
prisma = global.prisma;
}
export default prisma;
useLoaderData()
hook.// routes/index.js
import { useLoaderData, Link } from 'remix'
import prisma from '~/lib/db.server'
export const loader = async () => {
const data = {
teams: await prisma.team.findMany(),
}
return data
}
export default function Index() {
const { teams } = useLoaderData()
return (
<>
<div>
<h1>Soccer Teams around the world</h1>
</div>
<ul>
{teams.map((team) => (
<li key={team.id}>
<h1>{team.team}</h1>
<p>{team.country}</p>
</li>
))}
</ul>
</>
)
}
findMany()
function to get all the teams in our database.