Tôi đang cố gắng truy cập this.props
theo phương thức clicked()
, nhưng chúng không được xác định. Làm thế nào tôi có thể truy cập this.props
thông qua các phương pháp trong lớp ListItemExample?React Native không thể truy cập this.props bên ngoài phương thức render()
Mục tiêu của tôi là duy trì id
vào chế độ xem chương trình hiển thị chế độ xem chi tiết cho một ListItemExample được nhấp.
export default class Example extends Component {
constructor() {
super();
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds,
isLoading: true
};
}
componentDidMount() {
this.fetchData()
}
fetchData() {
Api.getPosts().then((resp) => {
console.log(resp);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(resp.posts),
isLoading: false
})
});
}
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
style={styles.postList}
/>
</View>
);
}
renderRow(post) {
return (
<PostListItem
id={post.id}
coverImage={post.cover_image}
title={post.title}
lockedStatus={post.status}
time={post.time} />
);
}
}
export default class ListItemExample extends Component {
constructor() {
super();
}
render() {
return (
<TouchableHighlight onPress={this.clicked} >
<View style={styles.postItem}>
</View>
</TouchableHighlight>
);
}
clicked() {
console.log("clicked props");
console.log(this.props);
Actions.openPost();
}
}
Không nên người xây dựng của bạn lấy 'đạo cụ' làm đối số và chuyển nó sang siêu? 'constructor (props) {super (props)}' – TAGraves