mobx在函数式组件中使用,props获取不到



  • https://github.com/NervJS/taro/issues/6787

    //app.tsx
    import Taro, { Config } from '@tarojs/taro'
    import { Provider } from '@tarojs/mobx'
    import Index from './pages/index'
    import counterStore from './store/counter'
    
    const store = {
      counterStore
    }
    
    function App() {
      return (
        <Provider store={store}>
          <Index />
        </Provider>
      )
    }
    
    App.config = {
      pages: [
        'pages/demo/index',
        'pages/index/index'
      ],
      window: {
        backgroundTextStyle: 'light',
        navigationBarBackgroundColor: '#ff0',
        navigationBarTitleText: 'WeChat',
        navigationBarTextStyle: 'black'
      },
      "networkTimeout": {
        "request": 10000,
        "connectSocket": 10000,
        "uploadFile": 10000,
        "downloadFile": 10000
      },
      "debug": false
    
    }
    Taro.render(<App />, document.getElementById('app'))
    
    // demo.tsx
    import Taro, { useState, useEffect } from '@tarojs/taro'
    import { View, Button, Text, Block } from '@tarojs/components'
    import ProTypes from 'prop-types';
    import { observer, inject } from '@tarojs/mobx'
    import { formatTime } from "../../utils/tool"
    import { userCenter } from '../../api/demo'
    
    const Demo = (props: any) => {
        const [count, setCount] = useState(0)
        useEffect(() => {
            console.log('props', props);
            console.log(formatTime(new Date().valueOf()));
            userCenter({
                pageNumber: 0,
                pageSize: 10
            }).then((res: any) => {
                console.log(res);
            })
    
            // const subscription = props.source.subscribe();
            return () => {
                // 清除订阅
                // subscription.unsubscribe();
            };
        }, [props]);
        const getUserInfos = (e: any) => {
            console.log((e));
        }
        return (
            <Block>
                <View>kk</View>
                <Button size='mini' openType='getUserInfo' onGetUserInfo={getUserInfos}>获取用户信息</Button>
                <Text>Count {count}</Text>
                <Button onClick={setCount.bind(this, 0)}>Reset</Button>
                <Button onClick={setCount.bind(this, (prevCount: number) => prevCount + 1)}>+</Button>
                <Button onClick={() => setCount(prevCount => prevCount + 1)}>+</Button>
                <Button onClick={() => setCount(prevCount => prevCount - 1)}>-</Button>
            </Block >
        )
    }
    export default inject('counterStore')(observer(Demo))
    
    Demo.config = {
        navigationBarTitleText: '测试页面'
    }
    Demo.proTypes = {
        count: ProTypes.number.isRequired,
        url: ProTypes.string
    };
    Demo.defaultProps = {
        count: 3
    }
    

    系统信息
    Taro v2.2.9

    Taro CLI 2.2.9 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: 2.2.9 => 2.2.9
    @tarojs/components-qa: 2.2.9 => 2.2.9
    @tarojs/mini-runner: 2.2.9 => 2.2.9
    @tarojs/mobx: 2.2.9 => 2.2.9
    @tarojs/mobx-h5: 2.2.9 => 2.2.9
    @tarojs/plugin-sass: 2.2.9 => 2.2.9
    @tarojs/plugin-terser: 2.2.9 => 2.2.9
    @tarojs/router: 2.2.9 => 2.2.9
    @tarojs/taro: 2.2.9 => 2.2.9
    @tarojs/taro-alipay: 2.2.9 => 2.2.9
    @tarojs/taro-h5: 2.2.9 => 2.2.9
    @tarojs/taro-qq: 2.2.9 => 2.2.9
    @tarojs/taro-quickapp: 2.2.9 => 2.2.9
    @tarojs/taro-swan: 2.2.9 => 2.2.9
    @tarojs/taro-tt: 2.2.9 => 2.2.9
    @tarojs/taro-weapp: 2.2.9 => 2.2.9
    @tarojs/webpack-runner: 2.2.9 => 2.2.9
    eslint-config-taro: 2.2.9 => 2.2.9
    eslint-plugin-taro: 2.2.9 => 2.2.9
    nerv-devtools: ^1.5.5 => 1.5.7
    nervjs: ^1.5.5 => 1.5.7
    stylelint-config-taro-rn: 2.2.9 => 2.2.9
    stylelint-taro-rn: 2.2.9 => 2.2.9



  • 已解决


登录后回复