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 渲染微信小程序失败 #7403

Closed
PresByter2015 opened this issue Aug 21, 2020 · 5 comments
Closed

函数式组件使用mobx 渲染微信小程序失败 #7403

PresByter2015 opened this issue Aug 21, 2020 · 5 comments
Assignees
Labels
T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@PresByter2015
Copy link

相关平台

微信小程序

小程序基础库: 2.12.1
使用框架: Nerv

复现步骤

function Log() {
    const [count, setCount] = useState(0);
    return (
        <View>
            Count: {count}
            <Button onClick={() => setCount(0)}>Reset</Button>
            <Button onClick={() => setCount(prevCount => prevCount   1)}> </Button>
            <Button onClick={() => setCount(prevCount => prevCount - 1)}>-</Button>
        </View>
    );
}
export default observer(Log);

小程序上报错

TypeError: react__WEBPACK_IMPORTED_MODULE_1__.d.useDebugValue is not a function
    at useObserver (vendors.js:978)
    at Component.wrappedComponent (vendors.js:1043)
    at Component.c.render (vendors.js:8645)
    at vendors.js:8694
    at errorCatcher (vendors.js:8582)
    at renderComponent (vendors.js:8693)
    at mountComponent (vendors.js:8666)
    at ComponentWrapper.init (vendors.js:9089)
    at createElement (vendors.js:8313)
    at mountVNode (vendors.js:8628)

期望结果

如何在taro函数式组件中正确使用mobx

实际结果

正确渲染

环境信息

 Taro CLI 3.0.7 environment info:
    System:
      OS: macOS 10.15
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.16.3 - /usr/local/bin/node
      Yarn: 1.22.4 - /usr/local/bin/yarn
      npm: 6.9.0 - /usr/local/bin/npm
    npmPackages:
      @tarojs/components: 3.0.7 => 3.0.7 
      @tarojs/mini-runner: 3.0.7 => 3.0.7 
      @tarojs/runtime: 3.0.7 => 3.0.7 
      @tarojs/taro: 3.0.7 => 3.0.7 
      @tarojs/webpack-runner: 3.0.7 => 3.0.7 
      babel-preset-taro: 3.0.7 => 3.0.7 
      eslint-config-taro: 3.0.7 => 3.0.7 
      nervjs: ^1.5.0 => 1.5.7 
@taro-bot2 taro-bot2 bot added F-nerv T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Aug 21, 2020
@PresByter2015
Copy link
Author

VM240:1 [MobX] You haven't configured observer batching which might result in unexpected behavior in some cases. See more at https://github.com/mobxjs/mobx-react-lite/#observer-batching
console.warn @ VM240:1
useObserver @ vendors.js:941
wrappedComponent @ vendors.js:1043
c.render @ vendors.js:8645
(anonymous) @ vendors.js:8694
errorCatcher @ vendors.js:8582
renderComponent @ vendors.js:8693
mountComponent @ vendors.js:8666
init @ vendors.js:9089
createElement @ vendors.js:8313
mountVNode @ vendors.js:8628
mountComponent @ vendors.js:8672
init @ vendors.js:9089
createElement @ vendors.js:8313
mountVNode @ vendors.js:8628
mountComponent @ vendors.js:8672
init @ vendors.js:9089
createElement @ vendors.js:8313
mountChild @ vendors.js:8372
mountVNode$1 @ vendors.js:8362
createElement @ vendors.js:8318
mountVNode @ vendors.js:8628
mountComponent @ vendors.js:8672
init @ vendors.js:9089
createElement @ vendors.js:8313
(anonymous) @ vendors.js:8331
createElement @ vendors.js:8330
patch @ vendors.js:7839
updateComponent @ vendors.js:8800
reRenderComponent @ vendors.js:8745
update @ vendors.js:9096
patch @ vendors.js:7826
updateComponent @ vendors.js:8800
reRenderComponent @ vendors.js:8745
update @ vendors.js:9096
patch @ vendors.js:7826
updateComponent @ vendors.js:8800
reRenderComponent @ vendors.js:8745
update @ vendors.js:9096
patch @ vendors.js:7826
updateComponent @ vendors.js:8800
forceUpdate @ vendors.js:8956
mount @ runtime.esm.js:3159
mount @ runtime.esm.js:3189
onLoad @ runtime.esm.js:2852
(anonymous) @ VM246 WAService.js:2
p.__callPageLifeTime__ @ VM246 WAService.js:2
jt @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
Mt @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
i.emit @ VM246 WAService.js:2
emit @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
i.emit @ VM246 WAService.js:2
emit @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
(anonymous) @ VM246 WAService.js:2
n @ VM244 asdebug.js:1
(anonymous) @ VM244 asdebug.js:1
(anonymous) @ VM244 asdebug.js:1
_ws.onmessage @ VM244 asdebug.js:1
Show 33 more frames
VM240:1 TypeError: react__WEBPACK_IMPORTED_MODULE_1__.d.useDebugValue is not a function
    at useObserver (vendors.js:978)
    at Component.wrappedComponent (vendors.js:1043)
    at Component.c.render (vendors.js:8645)
    at vendors.js:8694
    at errorCatcher (vendors.js:8582)
    at renderComponent (vendors.js:8693)
    at mountComponent (vendors.js:8666)
    at ComponentWrapper.init (vendors.js:9089)
    at createElement (vendors.js:8313)
    at mountVNode (vendors.js:8628)

如上报错,,如何在函数式组件中使用mobx

@rottenpen
Copy link
Contributor

image
你的代码真的这样写的吗?

@PresByter2015
Copy link
Author

image
你的代码真的这样写的吗?

`
function Index() {
const store = useLocalStore(() => ({
counter: 0,
increment() {
store.counter++
},
decrement() {
store.counter--
},
incrementAsync() {
setTimeout(() => store.counter++, 1000)
}
}))

const { counter, increment, decrement, incrementAsync } = store;
return (

+
-
Add Async
{counter}

)
}

export default observer(Index)
`
这样 也报错啊

@PresByter2015
Copy link
Author

引入是 NervJS,而不是react。

@Chen-jj Chen-jj added this to the 3.0.10 milestone Sep 3, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.10, 3.0.11 Sep 10, 2020
@ZakaryCode ZakaryCode modified the milestones: 3.0.11, 3.0.12 Sep 18, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.15, 3.0.16 Oct 23, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.16, 3.0.17 Nov 5, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.17, 3.0.18 Nov 17, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.18, 3.0.19 Dec 4, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.19, 3.0.20, 3.0.22 Dec 19, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.22, 3.1.0 Jan 4, 2021
@Chen-jj Chen-jj removed this from the 3.1.0 milestone Feb 19, 2021
@Chen-jj
Copy link
Contributor

Chen-jj commented Feb 19, 2021

Nerv 没有实现 useDebugValue 这个 hook,在使用函数式组件时 mobx 会调用它。所以 Nerv + mobx 目前只能使用 Class 组件。

@Chen-jj Chen-jj closed this as completed Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

No branches or pull requests

5 participants