Skip to content

Instantly share code, notes, and snippets.

View 0916dhkim's full-sized avatar

Danny Kim 0916dhkim

View GitHub Profile
export default function NewPost() {
const navigate = useNavigate();
const invalidateCache = useCacheInvalidate();
// ...
/** @type React.FormEventHandler */
const handleSubmit = async (e) => {
e.preventDefault();
await createPost({title, content});
invalidateCache('/posts');
navigate('/posts');
router.post(
'/',
handleErrors(async (req, res) => {
const post = await prisma.post.create({
data: {
title: req.body.title,
content: req.body.content,
},
});
export function useCacheInvalidate() {
const cache = unstable_getCacheForType(createResponseCache);
return (key) => {
cache.delete(key);
};
}
// Original
export function useServerResponse(location) {
const key = JSON.stringify(location);
const cache = unstable_getCacheForType(createResponseCache);
let response = cache.get(key);
if (response) {
return response;
}
response = createFromFetch(
fetch('/react?location=' + encodeURIComponent(key))
function sendResponse(req, res, component, props = null) {
props = props ?? {};
renderReactTree(res, component, props);
}
router.get('/', function(req, res) {
sendResponse(req, res, Home);
});
router.get('/posts', (req, res) => {
// Original demo.
async function renderReactTree(res, props) {
await waitForWebpack();
const manifest = readFileSync(
path.resolve(__dirname, '../build/react-client-manifest.json'),
'utf8'
);
const moduleMap = JSON.parse(manifest);
pipeToNodeWritable(React.createElement(ReactApp, props), res, moduleMap);
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const makeStyles = (theme: Theme) =>
StyleSheet.create({
url: {
color: theme.link,
textDecorationLine: 'underline',
},
});
@0916dhkim
0916dhkim / MyComponent.tsx
Created January 31, 2021 20:34
How to use Relay updater function
const contentMutation = graphql`
mutation MyComponentContentMutation(
$postId: ID!
) {
deletePostContent(postId: $postId) {
id
content
}
}
`;
@0916dhkim
0916dhkim / no-res-mutation.graphql
Created January 31, 2021 20:21
Mutation that does not respond with the updated record
type Mutation {
deletePostContent(postId: ID!): Boolean
}
@0916dhkim
0916dhkim / MyComponent.tsx
Created January 31, 2021 20:04
How to use mutation
const titleMutation = graphql`
mutation MyComponentTitleMutation(
$postId: ID!
$title: String!
) {
changePostTitle(postId: $postId, title: $title) {
id
title
}
}