MCPcopy
hub / github.com/BuilderIO/mitosis / contextToReact

Function contextToReact

packages/core/src/generators/context/react.ts:12–39  ·  view source on GitHub ↗
(options: ContextToReactOptions = { typescript: false, preact: false })

Source from the content-addressed store, hash-verified

10
11export const contextToReact =
12 (options: ContextToReactOptions = { typescript: false, preact: false }) =>
13 ({ context }: { context: MitosisContext }): string => {
14 let str = `
15 import { createContext } from '${options.preact ? 'preact' : 'react'}';
16
17 export default createContext${options.typescript ? '<any>' : ''}(${stringifyContextValue(
18 context.value,
19 )})
20 `;
21
22 if (options.format !== false) {
23 try {
24 str = format(str, {
25 parser: 'typescript',
26 plugins: [
27 require('prettier/parser-typescript'), // To support running in browsers
28 ],
29 });
30 } catch (err) {
31 if (process.env.NODE_ENV !== 'test') {
32 console.error('Format error for file:', str);
33 }
34 throw err;
35 }
36 }
37
38 return str;
39 };

Callers 1

context.test.tsFile · 0.90

Calls 2

stringifyContextValueFunction · 0.90
formatFunction · 0.85

Tested by

no test coverage detected