将函数作为 props 传递会在 React Native 中产生错误(onPress 不是函数,'onPress' 是 Object 的实例)
passing function as props generates an error in React Native(onPress is not a function, 'onPress' is an instance of Object)
我正在尝试从 child 组件触发 parent 组件中的某些操作,为此我正在使用道具。 child 的一个属性是一个函数,它在按下按钮时更新 parent 的状态,这应该导致 parent 到 re-render 但事实并非如此,相反,我收到此错误:TypeError: onPress is not a function. (In 'onPress(event)', 'onPress' is an instance of Object)
。我在 React 中使用函数作为 props 并多次更新状态,但在 React Native 中我似乎不明白是什么导致了这个问题以及我如何根据我在网上找到的信息解决它。
Child分量:
import * as React from 'react';
import {Avatar, Button, Card, Title, Paragraph } from 'react-native-paper';
const Photo:React.FC<{url:string, camera:string, handleClick:()=>void}> = ({url, camera}:{url:string, camera:string}, handleClick:()=>void) => (
<Card >
<Card.Content >
<Paragraph style={{color:'#6200ee'}}>{camera}</Paragraph>
</Card.Content>
<Card.Cover source={{ uri: url }} />
<Card.Actions>
<Button onPress={handleClick} mode='contained'>more</Button>
</Card.Actions>
</Card>
);
export default Photo;
Parent 分量:
import React, {useContext, useEffect, forwardRef, useState, Ref, useImperativeHandle, useRef} from 'react'
import { View, Text, ScrollView, StyleSheet } from 'react-native'
import { ActivityIndicator} from 'react-native-paper';
import {theContext} from '../utils/ContextPlaceholder'
import getPhotos from '../utils/getPhotos'
import Photo from './Photo'
import axios from 'axios';
import {PhotosViewV2} from './PhotosViewV2'
import ImageViewer from 'react-native-image-zoom-viewer';
interface RefObject {
getData: () => void
}
export const PhotosView = forwardRef((props, ref: Ref<RefObject>)=> {
const [photos, setPhotos]=useState<any[]>([])
const[clicked, setClicked]=useState(false)
const context=useContext(theContext)
const{rover, camera, year,month,day} =context
useImperativeHandle(ref, () => ({getData}));
async function getData() {
const photos=await getPhotos(1,rover, camera, year,month,day)
setPhotos(photos)
}
return(
<View>
<ScrollView>
{photos.map(({cam, url},idx)=>{
return <Photo handleClick={()=>setClicked(true)} camera={cam} key={idx} url={url}/>
})}
</ScrollView>
<PhotosViewV2 clickedFromOutside={clicked} data={photos}/>
</View>
);
});
问题出在这里({url, camera}:{url:string, camera:string}, handleClick:()=>void)
你从第一个参数得到url
和camera
,也就是props
,从第二个参数得到handleClick
,即 forwardRef
,这确实是一个对象。你需要像这样重写它:
({url, camera, handleClick}:{url:string, camera:string, handleClick:()=>void})
我正在尝试从 child 组件触发 parent 组件中的某些操作,为此我正在使用道具。 child 的一个属性是一个函数,它在按下按钮时更新 parent 的状态,这应该导致 parent 到 re-render 但事实并非如此,相反,我收到此错误:TypeError: onPress is not a function. (In 'onPress(event)', 'onPress' is an instance of Object)
。我在 React 中使用函数作为 props 并多次更新状态,但在 React Native 中我似乎不明白是什么导致了这个问题以及我如何根据我在网上找到的信息解决它。
Child分量:
import * as React from 'react';
import {Avatar, Button, Card, Title, Paragraph } from 'react-native-paper';
const Photo:React.FC<{url:string, camera:string, handleClick:()=>void}> = ({url, camera}:{url:string, camera:string}, handleClick:()=>void) => (
<Card >
<Card.Content >
<Paragraph style={{color:'#6200ee'}}>{camera}</Paragraph>
</Card.Content>
<Card.Cover source={{ uri: url }} />
<Card.Actions>
<Button onPress={handleClick} mode='contained'>more</Button>
</Card.Actions>
</Card>
);
export default Photo;
Parent 分量:
import React, {useContext, useEffect, forwardRef, useState, Ref, useImperativeHandle, useRef} from 'react'
import { View, Text, ScrollView, StyleSheet } from 'react-native'
import { ActivityIndicator} from 'react-native-paper';
import {theContext} from '../utils/ContextPlaceholder'
import getPhotos from '../utils/getPhotos'
import Photo from './Photo'
import axios from 'axios';
import {PhotosViewV2} from './PhotosViewV2'
import ImageViewer from 'react-native-image-zoom-viewer';
interface RefObject {
getData: () => void
}
export const PhotosView = forwardRef((props, ref: Ref<RefObject>)=> {
const [photos, setPhotos]=useState<any[]>([])
const[clicked, setClicked]=useState(false)
const context=useContext(theContext)
const{rover, camera, year,month,day} =context
useImperativeHandle(ref, () => ({getData}));
async function getData() {
const photos=await getPhotos(1,rover, camera, year,month,day)
setPhotos(photos)
}
return(
<View>
<ScrollView>
{photos.map(({cam, url},idx)=>{
return <Photo handleClick={()=>setClicked(true)} camera={cam} key={idx} url={url}/>
})}
</ScrollView>
<PhotosViewV2 clickedFromOutside={clicked} data={photos}/>
</View>
);
});
问题出在这里({url, camera}:{url:string, camera:string}, handleClick:()=>void)
你从第一个参数得到url
和camera
,也就是props
,从第二个参数得到handleClick
,即 forwardRef
,这确实是一个对象。你需要像这样重写它:
({url, camera, handleClick}:{url:string, camera:string, handleClick:()=>void})