-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Flight Fixture] Refetching #20316
[Flight Fixture] Refetching #20316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react'; | ||
|
||
import Container from './Container.js'; | ||
import useMutation from './useMutation.client'; | ||
|
||
export default function AddTodo() { | ||
const [text, setText] = React.useState(''); | ||
const [postTodo, isPosting] = useMutation(async () => { | ||
setText(''); | ||
await fetch('http://localhost:3001/todos', { | ||
method: 'POST', | ||
mode: 'cors', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({text}), | ||
}); | ||
}); | ||
return ( | ||
<Container> | ||
<input value={text} onChange={e => setText(e.target.value)} /> | ||
<button onClick={() => postTodo()}>add</button> | ||
{isPosting && ' ...'} | ||
</Container> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as React from 'react'; | ||
|
||
export const RefreshContext = React.createContext(null); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from 'react'; | ||
import useMutation from './useMutation.client'; | ||
|
||
export default function DeleteTodo({id}) { | ||
const [deleteTodo] = useMutation(async () => { | ||
await fetch('http://localhost:3001/todos/' + id, { | ||
method: 'DELETE', | ||
mode: 'cors', | ||
}); | ||
}); | ||
return <button onClick={() => deleteTodo()}>x</button>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,32 @@ import * as React from 'react'; | |
import {Suspense} from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import ReactTransportDOMClient from 'react-transport-dom-webpack'; | ||
import {RefreshContext} from './Context.client'; | ||
|
||
let data = ReactTransportDOMClient.createFromFetch( | ||
let initialData = ReactTransportDOMClient.createFromFetch( | ||
fetch('http://localhost:3001') | ||
); | ||
|
||
function Content() { | ||
return data.readRoot(); | ||
let [data, setData] = React.useState(initialData); | ||
|
||
function refresh() { | ||
setData( | ||
ReactTransportDOMClient.createFromFetch(fetch('http://localhost:3001')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we have it, this should use the client cache and it's the cache that gets reset. However, even in that world this can be optimized by seeding the client cache with a new entry. The idiomatic way would be to seed it with a response that comes from the same HTTP request as the mutation though. That last part you can prepare for now even without a client cache. You just need to pass this in to the refresh function as an argument. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be explicit. The reason for this is not perf but eventual consistency. You can easily get into a state where you never see your mutation in prod because the propagation is always slower than immediately handling the next request. That's a really bad user experience and bad developer experience to only know about it once you hit prod. |
||
); | ||
} | ||
|
||
return ( | ||
<RefreshContext.Provider value={refresh}> | ||
{data.readRoot()} | ||
</RefreshContext.Provider> | ||
); | ||
} | ||
|
||
ReactDOM.render( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. omg |
||
ReactDOM.unstable_createRoot(document.getElementById('root')).render( | ||
<Suspense fallback={<h1>Loading...</h1>}> | ||
<Content /> | ||
</Suspense>, | ||
document.getElementById('root') | ||
</Suspense> | ||
); | ||
|
||
// Create entry points for Client Components. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
|
||
import {RefreshContext} from './Context.client'; | ||
|
||
export default function useMutation(performMutation) { | ||
const [pendingCount, setPendingCount] = React.useState(0); | ||
const [startTransition, isTransitioning] = React.unstable_useTransition(); | ||
const refresh = React.useContext(RefreshContext); | ||
|
||
async function handleMutation(...args) { | ||
setPendingCount(c => c + 1); | ||
try { | ||
await performMutation(...args); | ||
} finally { | ||
setPendingCount(c => c - 1); | ||
} | ||
startTransition(() => { | ||
refresh(); | ||
}); | ||
} | ||
|
||
return [handleMutation, pendingCount > 0 || isTransitioning]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have this fetch return a new Flight response. It means you'll have to hard code which Flight root entry point this corresponds to. That's kind of a hard problem for a router but it's good to indicate that it is a problem that's worth solving.