跨框架解决方案-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>
  );
}
相关推荐
计算机-秋大田5 小时前
基于微信小程序的私家车位共享系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·微信小程序·小程序·课程设计
dal118网工任子仪6 小时前
93,【1】buuctf web [网鼎杯 2020 朱雀组]phpweb
android·前端
赛博末影猫7 小时前
Spring理论知识(Ⅴ)——Spring Web模块
java·前端·spring
苹果酱05677 小时前
Redis基础篇(万丈高楼平地起):核心底层数据结构
java·vue.js·spring boot·mysql·课程设计
Swift社区7 小时前
LeetCode - #196 删除重复的电子邮件并保留最小 ID 的唯一电子邮件
vue.js·算法·leetcode·swift
GISer_Jing8 小时前
DeepSeek 阐述 2025年前端发展趋势
前端·javascript·react.js·前端框架
prince_zxill8 小时前
RESTful 架构原则及其在 API 设计中的应用
前端·javascript·架构·前端框架·restful
禁默8 小时前
【学术投稿-2025年计算机视觉研究进展与应用国际学术会议 (ACVRA 2025)】从计算机基础到HTML开发:Web开发的第一步
前端·计算机视觉·html
Anlici10 小时前
强势DeepSeek——三种使用方式+推理询问指令😋
前端·人工智能·架构
geovindu10 小时前
D3.js Org Chart
开发语言·javascript·ecmascript