areslabs / alita

一套把React Native代码转换成微信小程序代码的转换引擎工具。我们不造轮子,不发明新框架,只是提供工具把RN扩展到微信小程序端。
https://areslabs.github.io/alita
MIT License
1.96k stars 130 forks source link

RN的Button有问题? #78

Closed fandych closed 4 years ago

fandych commented 4 years ago

当使用Button时, 编译正常, 在小程序端运行时报错;

import React from 'react';
import {View, Text, Button, TouchableOpacity} from 'react-native';
import {history} from '@areslabs/router';

const MainPage: React.FC<{}> = ({}) => {
    return (
        <View>
            <Text>Hello World</Text>
            <View>
                <TouchableOpacity onPress={()=>{
                    history.push("SecondPage")
                }}>
                    <Text>Click Me</Text>
                </TouchableOpacity>
                <Button
                    title="Click Me"
                    onPress={() => {
                        history.push("SecondPage")
                    }}
                />
            </View>
        </View>
    );
}

export default MainPage;

异常:

VM2318:1 TypeError: inst.render is not a function
    at updateClassComponent (index.js:1888)
    at render (index.js:1303)
    at renderPage (index.js:2451)
    at index.js:2951
console.error @ VM2318:1
render @ index.js:1311
renderPage @ index.js:2451
(anonymous) @ index.js:2951
Promise.then (async)
WxNormalComp.o.methods.onLoad @ index.js:2950
(anonymous) @ VM2324 WAService.js:2
p.__callPageLifeTime__ @ VM2324 WAService.js:2
jt @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
Mt @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
i.emit @ VM2324 WAService.js:2
emit @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
i.emit @ VM2324 WAService.js:2
emit @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
(anonymous) @ VM2324 WAService.js:2
n @ VM2322 asdebug.js:1
(anonymous) @ VM2322 asdebug.js:1
(anonymous) @ VM2322 asdebug.js:1
_ws.onmessage @ VM2322 asdebug.js:1
VM2318:1 (in promise) MiniProgramError
inst.getWxInst is not a function
TypeError: inst.getWxInst is not a function
    at Ge.<anonymous> (http://127.0.0.1:32560/appservice/_rn_.js:3984:29)
    at Ge.<anonymous> (http://127.0.0.1:32560/appservice/__dev__/WAService.js:2:1897073)
    at commitWork (http://127.0.0.1:32560/appservice/_rn_.js:3942:15)
    at renderPage (http://127.0.0.1:32560/appservice/_rn_.js:3898:3)
    at http://127.0.0.1:32560/appservice/_rn_.js:4390:9
canfoo commented 4 years ago

用的是哪个版本?

fandych commented 4 years ago

用的是哪个版本?

应该是最新的吧,最近才发现alita这个东西,在研究

canfoo commented 4 years ago

我大概知道什么原因,你这个是页面入口的组件吧,你把组件改成class声明,不要用functions声明,即试试这样:

export default class MainPage extends Component {

    render() {
        return (
            <View>
                <Text>Hello World</Text>
                <View>
                    <TouchableOpacity onPress={()=>{
                        history.push("SecondPage")
                    }}>
                        <Text>Click Me</Text>
                    </TouchableOpacity>
                    <Button
                        title="Click Me"
                        onPress={() => {
                            history.push("SecondPage")
                        }}
                    />
                </View>
            </View>
        )
    }

}
fandych commented 4 years ago

多谢, class声明可以正常使用