Skip to content

Commit

Permalink
fix(mobx): 修复h5下,componentDidShow不触发的问题 (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingboy authored and luckyadam committed Mar 26, 2019
1 parent cdd1370 commit 7076881
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 48 deletions.
47 changes: 18 additions & 29 deletions packages/taro-mobx-common/src/inject.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import { getStore } from './store'

const proxiedInjectorProps = {
isMobxInjector: {
value: true,
writable: true,
configurable: true,
enumerable: true
}
}

/**
* Store Injection
*/
function createStoreInjector (grabStoresFn, component, injectNames, { Component, createElement }) {
function createStoreInjector (grabStoresFn, injectNames, Component) {
let displayName =
'inject-' +
(component.displayName ||
component.name ||
(component.constructor && component.constructor.name) ||
(Component.displayName ||
Component.name ||
(Component.constructor && Component.constructor.name) ||
'Unknown')
if (injectNames) displayName += '-with-' + injectNames
if (injectNames) {
displayName += '-with-' + injectNames
}

class Injector extends Component {
static isMobxInjector = true
static displayName = displayName
render () {
constructor (props) {
let newProps = {}
for (let key in this.props) {
if (this.props.hasOwnProperty(key)) {
newProps[key] = this.props[key]
for (let key in props) {
if (props.hasOwnProperty(key)) {
newProps[key] = props[key]
}
}
const additionalProps = grabStoresFn(getStore() || {}, newProps, this.context) || {}
const additionalProps = grabStoresFn(getStore() || {}, newProps) || {}
for (let key in additionalProps) {
newProps[key] = additionalProps[key]
}
return createElement(component, newProps)
super(Object.assign(...arguments, newProps))
}
}

Object.defineProperties(Injector, proxiedInjectorProps)
Injector.config = component.config

return Injector
}

Expand All @@ -60,20 +50,19 @@ function grabStoresByName (storeNames) {
}
}

export function inject (/* fn(stores, nextProps) or ...storeNames, { Component, createElement } */) {
export function inject (/* fn(stores, nextProps) or ...storeNames */) {
let grabStoresFn
let platformSpecialParams = arguments[arguments.length - 1]
if (typeof arguments[0] === 'function') {
grabStoresFn = arguments[0]
return function (componentClass) {
return createStoreInjector(grabStoresFn, componentClass, null, platformSpecialParams)
return createStoreInjector(grabStoresFn, null, componentClass)
}
} else {
const storeNames = []
for (let i = 0; i < arguments.length - 1; i++) storeNames[i] = arguments[i]
for (let i = 0; i < arguments.length; i++) storeNames[i] = arguments[i]
grabStoresFn = grabStoresByName(storeNames)
return function (componentClass) {
return createStoreInjector(grabStoresFn, componentClass, storeNames.join('-'), platformSpecialParams)
return createStoreInjector(grabStoresFn, storeNames.join('-'), componentClass)
}
}
}
10 changes: 1 addition & 9 deletions packages/taro-mobx-h5/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import { Component } from '@tarojs/taro-h5'
import { createElement } from 'nervjs'
import { inject as originInject } from '@tarojs/mobx-common'

export function inject () {
return originInject(...arguments, { Component, createElement })
}

export { observer } from '@tarojs/mobx-common'
export { inject, observer } from '@tarojs/mobx-common'

export { default as Provider } from './Provider'
11 changes: 1 addition & 10 deletions packages/taro-mobx-rn/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
import { Component } from '@tarojs/taro-rn'
import { createElement } from 'react'
import { inject as originInject } from '@tarojs/mobx-common'

export function inject () {
return originInject(...arguments, { Component, createElement })
}

export { observer } from '@tarojs/mobx-common'

export { inject, observer } from '@tarojs/mobx-common'
export { default as Provider } from './Provider'

0 comments on commit 7076881

Please sign in to comment.