Javascript 如何在react中将id子组件传递给父组件?
我已经创建了一个自定义的列表组件,并在仪表板组件中使用。我已经导入了ListComponent并在其中传递了数据,列表将在仪表板组件中呈现。我有create函数来获取特定对象的id。因此,每当我单击任何列表时,都会在ListComponent中获得id。但是,我想在DashboardComponet上传递此id,但我无法这样做 ListComponet代码:Javascript 如何在react中将id子组件传递给父组件?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我已经创建了一个自定义的列表组件,并在仪表板组件中使用。我已经导入了ListComponent并在其中传递了数据,列表将在仪表板组件中呈现。我有create函数来获取特定对象的id。因此,每当我单击任何列表时,都会在ListComponent中获得id。但是,我想在DashboardComponet上传递此id,但我无法这样做 ListComponet代码: import React, { Component } from "react"; import {Text,View
import React, { Component } from "react";
import {Text,View} from "react-native";
import { List,ListItem} from "native-base";
export default class ListComponent extends Component {
constructor(props) {
super(props);
this.state = {
opID: "",
};
}
getUserId = (id) => {
console.log("id",id);
this.setState({opID:id})
};
render() {
return (
<View style={optionUI.container}>
<List>
{this.props.data.map((Opt, i) => (
<ListItem
key={Opt.id}
opID = {this.props.opID}
noBorder
onPress={() =>
this.getUserId(Opt.id)
}
>
<Text>{Opt.title}</Text>
</ListItem>
))}
</List>
</View>
);
}
}
使用道具
仪表板中的组件传递函数
<ListComponent data={userList.options} onClick = {this.onClickItem.bind(this)} />
在ListComponent类中调用函数onPress
getUserId = (id) => {
this.props.onClick(id) //Call the function
this.setState({opID:id})
};
<ListComponent data={userList.options} onClick = {this.onClickItem.bind(this)} />
onClickItem = (id) => {
console.log("id...",id);
}
getUserId = (id) => {
this.props.onClick(id) //Call the function
this.setState({opID:id})
};