为什么我的道具没有记录或在 React Native 中传递?

Why isn't my prop logging or being passed in React Native?

我制作了一个名为 IconButton 的组件,它接受一些 props 并传递任何额外的 props。

import Icon from 'react-native-vector-icons/FontAwesome';

const IconButton = ({ icon, title, ...props }) => {
    console.log(props); // Actual: {}, expected: { onPress: [function] }
    return (
        <View style={ iconBox } { ...props }>
           <Icon name={ icon } size={ 48 } color="black" />
           <Text>{ title }</Text>
        </View> 
};

然后我渲染了它:

const render = () => (
    <IconButton icon='plus' title='add' onPress={ () => console.log('hi') } />
);

然而,当我尝试用 console.log 登录时,onPress 没有出现;它记录了一个空对象。此外,它没有传递给我的 View,因为它在按下时没有调用 onPress。但是当我传递具有不同类型(例如数字和字符串)的不同道具时,它显示正常。

为什么它没有传递到我的 View 并且为什么没有记录 prop?如果这可能会影响任何事情,我也会使用 Expo。我已经设置了一个问题 on GitHub.

日志记录问题是 Expo 的一个错误,它使用 JSON.stringify 和其他方法提供输出,但没有正确输出对象。该错误还包含 onPress 等函数,它们是对象的一部分,并导致 console.log 根本不记录它,参见此处。该问题 on GitHub 并将在 2017 年 6 月 19 日的下一次 SDK 更新中修复。

现在换个话题,View 没有使用 onPress 道具,它不应该是可触摸的。要获得可触摸的视图,请尝试以下包装器: