-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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获取不了props #1313
Comments
欢迎提交 Issue~ 如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏 如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。 Good luck and happy coding~ |
@wokill |
import Taro from '@tarojs/taro'
import { observable, flow, configure, action } from 'mobx'
import { GetData, SetData, JWT, JWTEXPIRE } from '../utils/storage' // 缓存信息
import { UserInfo } from '../interfaces/user'
import delay from '../utils/delay'
// 设定数据只能在action 中改变
configure({ enforceActions: 'observed' })
interface register {
phone: string,
yzm: string,
[key: string]: any
}
export class User {
@observable userInfo: UserInfo
@observable uuid: string = ''
@observable jwt: string = GetData(JWT) || ''
login = flow(function * () {
// TODO:: 增加延时,测试代码,开发需替换
yield Taro.request({url: 'https://www.baidu.com'})
console.log('88888')
SetData(JWT, 'jwt', JWTEXPIRE)
this.jwt = 'jwt'
})
register = flow(function * (regData: register) {
console.log(regData)
this.uuid = '456'
})
@action
sendSms(phone: string) {
//
}
}
export default new User()
|
@wokill 无法复现,能把你的demo完整代码给发一下吗?[email protected] |
@nanjingboy 又尝试了几次,发现在组件 |
@wokill 你能把完整的demo发到我的邮箱吗?这样方便快速定位问题 |
已发送到你的邮箱里面了,请注意查收一下 |
@wokill 已修复,等官方发布新版本后你可以再试下 |
嗯,非常感谢 |
另外再咨询一个相关的问题,该问题只发生在 // login
export default function weappLogin (lifecycle: string = 'willMount') {
if (!LIFE_CYCLE_MAP.includes(lifecycle)) {
return component => component
}
return function withLogin(component) {
@inject('user')
class withLoginComponent extends component {
//TODO:: componentWillMount 当中使用了await 导致外层mobx观察数据发生改变,ui层不重新渲染。该问题只发生在 componentWillMount生命周期中
async componentWillMount() {
if (super.componentWillMount && lifecycle === LIFE_CYCLE_MAP[0]) {
//TODO:: 此处使用了 await 去除掉await无异常。
const res = await this.autoLogin()
if (!res) return
}
super.componentWillMount && super.componentWillMount()
}
async componentDidMount() {
if (super.componentDidMount && lifecycle === LIFE_CYCLE_MAP[1]) {
const res = await this.autoLogin()
if (!res) return
}
super.componentDidMount && super.componentDidMount()
}
async componentDidShow() {
if (super.componentDidShow && lifecycle === LIFE_CYCLE_MAP[2]) {
const res = await this.autoLogin()
if (!res) return
}
super.componentDidShow && super.componentDidShow()
}
async autoLogin () {
const { user } = this.props
if (user.jwt) {
return true;
}
await user.login()
if (user.jwt) {
return true;
}
Taro.showModal({title: '登录提醒', content: '当前操作需要登录后继续使用',confirmText: '前往登录', showCancel: false}).then(() => {
Taro.navigateTo({url: '/pages/login/index'})
})
return false
}
render() {
super.render();
}
}
return withLoginComponent
}
} 使用 // home/index.js
import Taro, { Component,Config } from '@tarojs/taro'
import { View, Image, Text } from '@tarojs/components'
import { observer, inject } from '@tarojs/mobx'
import { AtIcon } from 'taro-ui'
import { share, login } from '../../decorator'
import { User } from '../../store/declare'
import './index.scss'
interface HomeProps {
user: User
}
@share()
@login()
@inject('user')
@observer
export default class Home extends Component<HomeProps, {}> {
static defaultProps = {} as HomeProps
config: Config = {
navigationBarTitleText: '我的'
}
constructor(props) {
super(props);
}
componentWillMount() {
const { user } = this.props
// TODO::加载用户详情数据, 此处加载完用户数据之后,改边userInfo后,该页面并不会重新渲染
user.loadInfo()
}
componentDidMount() {
}
navigateTo(url: string) {
Taro.navigateTo({
url: url
})
}
// 提现
onBalance() {
const {user: { balance}} = this.props
if ( balance < 0 ) {
Taro.showToast({
title: '暂时没有可提现金额',
icon: 'none'
})
return
}
this.navigateTo('/pages/capital/balance')
}
render() {
const {user: {userInfo: {nickName, avatarUrl, phone}, frozen, cashed, balance}} = this.props
console.log(nickName,balance);
return (
<View className='home_container'>
</View>
);
}
} // user.js mobx
import Taro from '@tarojs/taro'
import { observable, flow, configure, action } from 'mobx'
import { GetData, SetData, JWT, JWTEXPIRE } from '../utils/storage' // 缓存信息
import { UserInfo, PushEl, ReceEl } from '../interfaces/user'
import delay from '../utils/delay'
// 设定数据只能在action 中改变
configure({ enforceActions: 'observed' })
interface register {
phone: string,
yzm: string,
[key: string]: any
}
const user = {
name: '章三',
phone: '18080093730',
nickName: '飞象',
city: '成都',
sign: '才不要些什么签名',
balance: 500,
cashed: 1000.50,
frozen: 268.36,
avatarUrl: 'http://storage.360buyimg.com/mtd/home/32443566_635798770100444_2113947400891531264_n1533825816008.jpg'
}
class User {
@observable uuid: string = ''
@observable jwt: string = GetData(JWT) || ''
@observable balance: number = 0
@observable cashed: number = 0
@observable frozen: number = 0
@observable userInfo: UserInfo
constructor() {
this.userInfo = {} as UserInfo
}
// 登录
login = flow(function * () {
// TODO:: 增加延时,测试代码,开发需替换
yield Taro.request({url: 'https://www.baidu.com'})
console.log('88888')
SetData(JWT, 'jwt', JWTEXPIRE)
this.jwt = 'jwt'
})
// 用户信息
loadInfo = flow(function * () {
const { balance, cashed, frozen } = user
this.cashed = cashed
this.balance = balance
this.frozen = frozen
this.userInfo = user
console.log(this.balance)
})
// 注册
register = flow(function * (regData: register) {
console.log(regData)
this.uuid = '456'
})
// 更新用户数据
updateData = flow(function * (attr: string, value: string) {
let info = this.userInfo
if (info.hasOwnProperty(attr)) {
// TODO:: 缺少发起更改请求
info[attr] = value
this.userInfo = Object.assign({}, this.userInfo, info)
return true
}
return false
})
// 修改手机号码
updatePhone = flow(function * (phone:string, code: string) {
//TODO:: 修改手机号码
return true
})
// 提现申请
sendCashed = flow(function *() {
// 更新userInfo 金额
return true
})
// 发送验证码
@action
sendSms(phone: string) {
//
}
}
export default new User() |
@wokill 那这个问题跟这个issue无关了,把你的demo发给我,有空看下。 |
OK,我重发一份到你邮箱,我新开一个issue |
@wokill ok,有空的话我看一下 |
问题描述
更新到1.2.0-beta.6 时使用高阶组件,在高阶组件当中使用mobx @Inject时丢失props。在1.2.0-beta.4时该问题不存在。。
复现步骤
期望行为
希望能正常获取通过@Inject 注入到props当中
报错信息
[这里请贴上你的完整报错截图或文字]
系统信息
补充信息
The text was updated successfully, but these errors were encountered: