为什么我不能在图像中使用状态
<Image source={require(this.state.image)} style={styles.img} />
我想更改图像,当我单击按钮时从数组中获取它,抽奖功能会更改图像状态,一切正常,但我无法将状态作为必需参数传递,如何解决?
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Image
} from 'react-native';
import Button from './src/components/Button.js';
export default class App extends Component{
constructor(props){
super(props);
this.state = {
image: './src/assets/interrogacao.png',
}
this.imagesArray = [
'./src/assets/1.jpg',
'./src/assets/2.jpg',
'./src/assets/3.jpg',
]
this.rafflePosition = this.rafflePosition.bind(this);
}
rafflePosition(){
let state = this.state;
let numRandom = Math.floor(Math.random() * this.imagesArray.length)
this.state.image = this.imagesArray[numRandom];
var teste = this.state.image;
this.setState(state);
alert('teste' + teste)
}
render() {
return (
<View style={styles.container}>
<Image source={require(this.state.image)} style={styles.img} />
<Button onPress={this.rafflePosition} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
img: {
width: 220,
height: 220,
marginBottom: 90
}
});