【发布时间】:2016-07-27 12:00:22
【问题描述】:
我试图在clicked() 方法中访问this.props,但它们是未定义的。如何通过 ListItemExample 类中的方法访问this.props?
我的目标是将id 维护到显示单击的ListItemExample 的详细视图的显示视图中。
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();
}
}
【问题讨论】:
-
你的构造函数不应该把
props作为参数传递给super吗?constructor(props) { super(props) }
标签: javascript reactjs react-native