Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在其他组件中使用item.key而不是alert_Javascript_Reactjs_React Native_React Native Android - Fatal编程技术网

Javascript 如何在其他组件中使用item.key而不是alert

Javascript 如何在其他组件中使用item.key而不是alert,javascript,reactjs,react-native,react-native-android,Javascript,Reactjs,React Native,React Native Android,FlatList组件工作完成,如果我单击Android,它将在Android中的aler中显示,但我必须将绑定值传递到Subscribe组件,而不是alert。 我是否必须将数据传递到状态,并将其用作其他组件中的道具 import React, { Component } from 'react'; import { AppRegistry, FlatList, StyleSheet, Text, View,Alert } from 'react-native'; export def

FlatList组件工作完成,如果我单击Android,它将在Android中的aler中显示,但我必须将绑定值传递到Subscribe组件,而不是alert。 我是否必须将数据传递到状态,并将其用作其他组件中的道具

import React, { Component } from 'react';  
import { AppRegistry, FlatList, StyleSheet, Text, View,Alert } from 'react-native';  

export default class FlatList extends Component {  

    constructor(props) {
        super(props);
        this.updateKey = this.updateKey.bind(this);
        this.state = {};
    }

    updateKey(keyData) {
        this.setState({ keyData: keyData });
    }

    getListViewItem = (item) => {  
        Alert.alert(item.key);  
    }  

    render() {  
        return (  
            <View style={styles.container}>  
                <FlatList  
                    data={[  
                        {key: 'Android'},{key: 'iOS'}, {key: 'Java'},{key: 'Swift'},  
                        {key: 'Php'},{key: 'Hadoop'},{key: 'Sap'},  
                    ]}  
                    renderItem={({item}) =>  
                        <Text style={styles.item}  
                              onPress={this.getListViewItem.bind(this, item)}>{item.key}</Text>}          
                />  
            </View>  
        );  
    }  
}

const styles = StyleSheet.create({  
    container: {  
        flex: 1,  
    },  
    item: {  
        padding: 10,  
        fontSize: 18,  
        height: 44,  
    },  
})  
AppRegistry.registerComponent('flatList', () => FlatList);
import React,{Component}来自'React';
从“react native”导入{AppRegistry、FlatList、样式表、文本、视图、警报};
导出默认类平面列表扩展组件{
建造师(道具){
超级(道具);
this.updateKey=this.updateKey.bind(this);
this.state={};
}
updateKey(关键数据){
this.setState({keyData:keyData});
}
getListViewItem=(项)=>{
警报。警报(项。键);
}  
render(){
报税表(
{item.key}
/>  
);  
}  
}
constyles=StyleSheet.create({
容器:{
弹性:1,
},  
项目:{
填充:10,
尺码:18,
身高:44,
},  
})  
AppRegistry.registerComponent('flatList',()=>flatList);
import React,{Component}来自“React”
从“react native”导入{View,Item};
导出默认类订阅扩展组件{
render(){
返回(
{item.key}
)
}
}

是的,如果您想将其作为道具传递给子组件,可以将其设置在您的状态中,并将其传递给您正在谈论的子组件

要做到这一点,您只需将
警报更改为:
this.setState({selectedValue:item})

那么你的孩子看起来会像:

<Child item = {this.state.selectedValue} />

是的,如果您想在另一个组件中使用该值,您必须将其作为prop传递。我如何使用?您能给我发送代码吗?重新格式化的代码。使用```语法切断了其中的一部分
<Child item = {this.state.selectedValue} />
this.props.item