main
js 38 lines 1.17 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9 import {createContext, useContext} from 'react';
10
11 // Note: this file does not demonstrate a real data fetching strategy.
12 // We only use this to simulate data fetching happening on the server
13 // while the cache is populated on the client. In a real app, you would
14 // instead use a data fetching library or Server Components for this.
15
16 const DataContext = createContext(null);
17
18 export function DataProvider({children, data}) {
19 return <DataContext.Provider value={data}>{children}</DataContext.Provider>;
20 }
21
22 // In a real implementation the data would be streamed with the HTML.
23 // We haven't integrated this part yet, so we'll just use fake data.
24 const fakeData = [
25 "Wait, it doesn't wait for React to load?",
26 'How does this even work?',
27 'I like marshmallows',
28 ];
29
30 export function useData() {
31 const ctx = useContext(DataContext);
32 if (ctx !== null) {
33 // This context is only provided on the server.
34 // It is here to simulate a suspending data fetch.
35 ctx.read();
36 }
37 return fakeData;
38 }