Last active
January 17, 2022 16:07
-
-
Save davist11/87cdd99eb66d01f2a9dbc026324f9550 to your computer and use it in GitHub Desktop.
Remix Blog Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
useActionData, | |
Form, | |
redirect, | |
json | |
} from 'remix' | |
export async function action({ request }) { | |
const formData = await request.formData() | |
const errors = {} | |
// Do data validation and add to the errors object if there are any errors | |
if (Object.keys(errors).length) { | |
return json(errors, { status: 422 }) | |
} | |
// No errors: send email, store in DB, do whatever you need to do with the form data | |
return redirect('/contact/thanks') | |
} | |
export default function ContactIndex() { | |
const errors = useActionData() | |
return ( | |
<Form method="post"> | |
</Form> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLClient } from 'graphql-request' | |
import { gql } from 'graphql-request' | |
import { useLoaderData, json } from 'remix' | |
const endpoint = 'https://your-endpoint-here/' | |
const options = { | |
headers: { | |
authorization: 'Bearer YOUR_AUTH_TOKEN_HERE', | |
} | |
} | |
const EntriesQuery = gql` | |
{ | |
YOUR GRAPHQL QUERY HERE | |
} | |
` | |
export const loader = async () => { | |
const { entries } = new GraphQLClient(endpoint, options).request(EntriesQuery) | |
return json({ | |
entries | |
}) | |
} | |
export default function Index() { | |
const data = useLoaderData() | |
return ( | |
<> | |
{data.entries.map((entry) => ( | |
<div key={entry.id}> | |
{entry.title} | |
</div> | |
))} | |
</> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment