跨框架解决方案-Mitosis【Context】

Context

Mitosis的context必须是:

  • 在自己的文件中创建
  • 文件名必须以context.lite.ts结尾
  • 默认导出必须是一个返回context对象的函数
js 复制代码
// simple.context.lite.ts
import { createContext } from '@builder.io/mitosis';

export default createContext({
  foo: 'bar',
  get fooUpperCase() {
    return this.foo.toUpperCase();
  },
  someMethod() {
    return this.fooUpperCase.toLowercase();
  },
  content: null,
  context: {} as any,
  state: {},
});

然后你可以在组件中使用它

js 复制代码
import Context from './simple.context.lite';
import { useContext, setContext } from '@builder.io/mitosis';

export default function ComponentWithContext(props: { content: string }) {
  // you can access the context using `useContext`
  const foo = useContext(Context);

  // you can use `setContext` to provide a new value for the context
  setContext(Context, {
    foo: 'baz',
    content() {
      return props.content;
    },
  });

  return (
    // you can also use `Context.provider` to provide a new value for the context
    <Context.Provider value={{ bar: 'baz' }}>{foo.value}</Context.Provider>
  );
}
相关推荐
ljt272496066110 小时前
Vue笔记(十二)--mitt
前端·笔记
传人once1 天前
页面中心圆圈放大效果如何写
开发语言·javascript·ecmascript
用户033307413951 天前
TypeScript using 与 AsyncDisposableStack:在真实服务端代码中协调多资源清理
前端
web3d5201 天前
Vue3 地图白屏排查清单:容器、弹窗与 ol-viewport 重复创建
vue.js·gis
daisychey1 天前
用 Playwright 做多平台内容分发,我踩过的 8 个坑
前端
IT_陈寒1 天前
为什么你应该学习JavaScript?
前端·人工智能·后端
大龄秃头程序员1 天前
iOS冷启动监控Demo
前端
光影少年1 天前
为什么 JavaScript 中 0.1 + 0.2 !== 0.3,如何让其相等?
前端·javascript·算法
掘金酱1 天前
[稀土掘金 × 火山引擎] AI用量周榜冲刺赛|获奖名单公示
前端·人工智能·后端
泡泡oO1 天前
“如果你还在用Superpowers,那我不要和你说话”
前端·后端·全栈