我的react-native变量出现问题。当我用console.log()
显示变量时,一切都很好,我得到了它的内容。但是,当我将其与map
一起使用时,我的变量是不确定的。这是我的代码。
const ServiceAction = (props) => {
const { services } = props;
console.log("Services:", services)
return services.map((service, index) => {return <Text key={index}>{service.name}</Text>})
}
因此,当我将服务与地图一起使用时,这里的服务是不确定的,但在显示时却没有。
我刚刚调用了函数。
const Dashboard: () => React$Node = (props) => {
// const [state, dispatch] = React.useReducer(userReducer, props.navigation.getParam("user"));
// const ip = props.navigation.getParam("ip");
const ip = "192.168.1.37";
const [state, dispatch] = React.useReducer(userReducer, initialStateUser);
const [services, setServices] = React.useState([]);
const [serviceAction, setServiceAction] = React.useState("Default");
React.useEffect(() => {
fetch(`http://${ip}:8080/about.json`)
.then((response) => {return response.json()})
.then((data) => setServices(data.server.services))
.catch((err) => console.log(err))
},[])
return (
<LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 1}} colors={['rgba(58,115,215,1)', 'rgba(44,120,215,1)', 'rgba(68,116,213,1)', 'rgba(47,105,191,1)', 'rgba(131,75,191,1)', 'rgba(198,98,237,1)']} style={styles.linearGradient}>
<Image style={{width: 400, height: 50}} source={require('../assets/logo.png')}/>
<ServiceAction services={services} />
</LinearGradient>
)
}
如果有人有主意...
编辑:
服务是JSON格式
Services = [{
name: 'toto'
}, {
name: 'titi'
}
]
答案 0 :(得分:0)
尝试这样:
const ServiceAction = (props) => {
const { services } = props;
let testArray = []
console.log("Services:", services)
services.map((service, index) => {testArray.push(<Text key={index}>{service.name}</Text>)})
return testArray
}