Javascript Promise:.then()在从React本机组件Prop函数返回Promise时未被调用
我有一个react本机组件,如下所示:Javascript Promise:.then()在从React本机组件Prop函数返回Promise时未被调用,javascript,android,ios,reactjs,react-native,Javascript,Android,Ios,Reactjs,React Native,我有一个react本机组件,如下所示: export const SearchBar = (props: SearchListProps) => { const { data, matchingFunction, listItemVariant, onChange, onSubmit, renderItem, ...rest } = props; const animatedValue = useRef(new Anima
export const SearchBar = (props: SearchListProps) => {
const {
data,
matchingFunction,
listItemVariant,
onChange,
onSubmit,
renderItem,
...rest
} = props;
const animatedValue = useRef(new Animated.Value(0));
const [searchValue, setSearchValue] = useState('');
const [isFocused, setFocused] = useState(false);
const listData = useRef([...data]);
if (isFocused) {
Animated.timing(animatedValue.current, {
duration: 100,
toValue: 1,
useNativeDriver: true,
}).start();
}
useEffect(() => {
const promise = props?.dataSource(searchValue);
console.log('RETURNED: ' + JSON.stringify(promise));
promise
.then((result: any) => {
console.log('RESULT: ' + JSON.stringify(result));
})
.catch((e) => console.log(e));
}, [searchValue]);
//... rest of the code
然后我在某处使用这个组件,如下所示:
<SearchBar
listItemVariant={'withImage'}
dataSource={async (query?: string) => {
console.log('Query Passed: ' + query);
return new Promise<ListItem[]>((resolve, _) => {
resolve([
{
title: 'Paneer Bhurji',
subTitle: 'Delicious',
imageSource:
'https://homepages.cae.wisc.edu/~ece533/images/peppers.png',
},
{ title: 'Paneer Butter Masala', subTitle: 'High Protein' },
{
title: 'Chicken',
imageSource:
'https://homepages.cae.wisc.edu/~ece533/images/peppers.png',
},
]);
});
}}
// ...rest of the code
{
log('通过的查询:'+查询);
返回新的承诺((解决,))=>{
决心([
{
标题:“潘尼尔·布尔吉”,
副标题:"美味",,
图像来源:
'https://homepages.cae.wisc.edu/~ece533/images/peppers.png',
},
{标题:“Paneer Butter Masala”,副标题:“高蛋白”},
{
标题:"鸡",,
图像来源:
'https://homepages.cae.wisc.edu/~ece533/images/peppers.png',
},
]);
});
}}
//…代码的其余部分
问题是组件中没有调用.then()。
Console打印以下语句:
其他日志(在useEffect的.then()中)没有被打印。因此本质上,.then()方法从未被调用。您是否尝试过使用const promise=()=>props?.dataSource(searchValue);我试过了。log(“返回:+JSON.stringify(promise));现在打印“返回:未定义”