首页 > 解决方案 > React Native + Expo:将重点放在 useEffect 在 Android Box 中不起作用

问题描述

我在 React-Native 中创建了一个简单的主页,其中包含一个应该自动聚焦的图像,因此当用户单击 Android Box 的中心按钮时,它会转到另一个页面。

此代码适用于我计算机中的 Expo,但是当我在 Android Box 中的 Expo Go 上对其进行测试时,出现错误:

未捕获的错误:TypeError:l.current.focus 不是函数。(在 'l.current.focus()' 中,'l.current.focus' 未定义)

我的代码如下:

import { StyleSheet, Image, View, TouchableWithoutFeedback } from 'react-native';
import * as React from 'react';

const HomeScreen = ({navigation}) => {

  const goToNextScreen = React.useRef()

  React.useEffect(() => {
    goToNextScreen.current.focus()
  }, []);

  return (
      <View style={styles.container}>
        <TouchableWithoutFeedback
            ref={goToNextScreen}
            onPressOut={() => navigation.navigate('NextPage')}>
            <Image
                source={require('../assets/img/logo.png')}
            />
        </TouchableWithoutFeedback>
      </View>
  );
}

export default HomeScreen;

有人可以指出我正确的方向吗?

标签: androidreactjsreact-nativeexpo

解决方案


推荐阅读