您好,我正在为一家公司进行编程挑战,因为我必须开发一个像对宠物的本机应用程序这样的火种,我被困在无法看到服务器上任何图像的地方,我在componentDidMount中调用了正确的端点。我正在按照本教程https://www.youtube.com/watch?v=mmHywzVkKn8进行操作,因此我对反应/反应本地化还很陌生,我对angualar2有一定的经验,在那里我学会了使用async / await。据我有限的理解
这个问题确实是由于以下事实造成的:图像未按时渲染,因为视图确实会尽早初始化,因此我正在使用它来开发iOS应用。
import React from 'react';
import {StyleSheet, Text, View, Dimensions, Image, Animated, PanResponder} from 'react-native';
const SCREEN_HEIGHT = Dimensions.get('window').height;
const SCREEN_WIDTH = Dimensions.get('window').width;
export default class Pets extends React.Component {
constructor(){
super();
this.position = new Animated.ValueXY()
this.state = {
data: [],
currentIndex: 0
};
}
async componentDidMount(){
const response = await fetch("https://s3-us-west-2.amazonaws.com/cozi-interview-dev/pets.json")
const json = await response.json();
this.setState({ data: json });
}
componentWillMount() {
this.PanResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderMove: (evt, gestureState) => {
this.position.setValue({ x: gestureState.dx, y: gestureState.dy })
},
onPanResponderRelease: (evt, gestureState) => {
}
})
}
renderPets = () => {
return this.state['data'].map((item, i) => {
if (i < this.state.currentIndex) {
return null
}
else if (i == this.state.currentIndex) {
return (
<Animated.View
{...this.PanResponder.panHandlers}
key={item.id} style={[this.rotateAndTranslate, { height: SCREEN_HEIGHT - 120, width: SCREEN_WIDTH, padding: 10, position: 'absolute' }]}>
<Animated.View style={{ opacity: this.likeOpacity, transform: [{ rotate: '-30deg' }], position: 'absolute', top: 50, left: 40, zIndex: 1000 }}>
<Text style={{ borderWidth: 1, borderColor: 'green', color: 'green', fontSize: 32, fontWeight: '800', padding: 10 }}>LIKE</Text>
</Animated.View>
<Animated.View style={{ opacity: this.dislikeOpacity, transform: [{ rotate: '30deg' }], position: 'absolute', top: 50, right: 40, zIndex: 1000 }}>
<Text style={{ borderWidth: 1, borderColor: 'red', color: 'red', fontSize: 32, fontWeight: '800', padding: 10 }}>NOPE</Text>
</Animated.View>
<Image
style={{ flex: 1, height: null, width: null, resizeMode: 'cover', borderRadius: 20 }}
source={item.uri} />
</Animated.View>
)
}
else {
return (
<Animated.View
key={item.id} style={[{
opacity: this.nextCardOpacity,
transform: [{ scale: this.nextCardScale }],
height: SCREEN_HEIGHT - 120, width: SCREEN_WIDTH, padding: 10, position: 'absolute'
}]}>
<Animated.View style={{ opacity: 0, transform: [{ rotate: '-30deg' }], position: 'absolute', top: 50, left: 40, zIndex: 1000 }}>
<Text style={{ borderWidth: 1, borderColor: 'green', color: 'green', fontSize: 32, fontWeight: '800', padding: 10 }}>LIKE</Text>
</Animated.View>
<Animated.View style={{ opacity: 0, transform: [{ rotate: '30deg' }], position: 'absolute', top: 50, right: 40, zIndex: 1000 }}>
<Text style={{ borderWidth: 1, borderColor: 'red', color: 'red', fontSize: 32, fontWeight: '800', padding: 10 }}>NOPE</Text>
</Animated.View>
<Image
style={{ flex: 1, height: null, width: null, resizeMode: 'cover', borderRadius: 20 }}
source={item.uri} />
</Animated.View>
)
}
}).reverse()
}
render() {
return (
<View style={{ flex: 1 }}>
<View style={{ height: 60 }}>
</View>
<View style={{ flex: 1 }}>
{this.renderPets()}
</View>
<View style={{ height: 60 }}>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
答案 0 :(得分:1)
这是错误的source={item.uri}
,请执行source={{uri:item.img}}
。如下所示编辑您的图像源,然后Image
将呈现。
<Image
style={{
flex: 1,
height: null,
width: null,
resizeMode: 'cover',
borderRadius: 20,
}}
source={{uri:item.img}} <---This is the way add image
/>