Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序里 mobx inject 不支持 hooks 组件 #4676

Closed
ickeep opened this issue Oct 24, 2019 · 7 comments
Closed

小程序里 mobx inject 不支持 hooks 组件 #4676

ickeep opened this issue Oct 24, 2019 · 7 comments

Comments

@ickeep
Copy link
Contributor

ickeep commented Oct 24, 2019

问题描述
小程序里 mobx inject 不支持 hooks 组件

import Taro from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'
import { inject, observer } from '@tarojs/mobx'
import Store, { User } from '../../store'
import './index.less'

function Index(props: { user: User }) {
  // props 为空对象 inject 后取不到 user 的值
  // const { user: { user, setUser } } = props 报错
  const { user: { user, setUser } = Store.user } = props // 临时解决方案

  return (
    <View className='index'>
      <Text>Hello world!{user.id} 111 {user.name}</Text>
      <Button onClick={() => setUser({ id: 1, name: '1234' })}>setUser</Button>
      <Button onClick={() => setUser({ id: '', name: '' })}>setUser</Button>
    </View>
  )
}

Index.config = {
  navigationBarTitleText: '首页'
}

export default inject('user')(observer(Index))

期望行为
小程序里 mobx inject 与 h5 一致 支持 hooks 组件

报错信息

image

系统信息
Taro CLI 1.3.20 environment info:
System:
OS: macOS 10.15
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 10.16.0 - /usr/local/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm

@taro-bot
Copy link

taro-bot bot commented Oct 24, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@nanjingboy
Copy link
Member

@ickeep inject 只在类组件中可用

@suiyi8760
Copy link

那请问用hooks的话怎么inject呢

@nanjingboy
Copy link
Member

@suiyi8760 用 hooks 就能获得 store 信息了,要 inject 干吗?看这里的例子:https://github.com/nanjingboy/taro-mobx-sample

@suiyi8760
Copy link

@suiyi8760 用 hooks 就能获得 store 信息了,要 inject 干吗?看这里的例子:https://github.com/nanjingboy/taro-mobx-sample

是用createContext?这个api不是只支持微信小程序吗

@hlskysong
Copy link

@suiyi8760 请问这个问题你解决了吗? taro2 函数式组件如何 inject 呢

@nanjingboy
Copy link
Member

@hlsky1988 inject 不支持函数组件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants