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

Context

Mitosis的context必须是:

  • 在自己的文件中创建
  • 文件名必须以context.lite.ts结尾
  • 默认导出必须是一个返回context对象的函数
// 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: {
   },
});

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

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>
  );
}

相关推荐

  1. 框架解决方案-Mitosis【Context】

    2023-12-08 23:26:02       65 阅读
  2. 域和解决方案

    2023-12-08 23:26:02       34 阅读
  3. 域问题解决方案

    2023-12-08 23:26:02       33 阅读
  4. 域的解决方案

    2023-12-08 23:26:02       33 阅读
  5. 项目记录:域问题解决方案

    2023-12-08 23:26:02       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 23:26:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 23:26:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 23:26:02       87 阅读
  4. Python语言-面向对象

    2023-12-08 23:26:02       96 阅读

热门阅读

  1. vue在哪个生命周期内调用异步请求

    2023-12-08 23:26:02       54 阅读
  2. 【算法专题】分治 - 快速排序

    2023-12-08 23:26:02       40 阅读
  3. 合并表记录

    2023-12-08 23:26:02       56 阅读
  4. 【例1.6】回文数(Noip1999)

    2023-12-08 23:26:02       58 阅读
  5. Mysql日志binlog、redolog、undolog

    2023-12-08 23:26:02       48 阅读
  6. 人工智能在医疗领域的应用与前景

    2023-12-08 23:26:02       59 阅读
  7. LLVM学习笔记(64)

    2023-12-08 23:26:02       38 阅读