【发布时间】:2018-02-24 22:41:32
【问题描述】:
无法从渲染中调用函数。我的组件是:
class MyComp extends Component {
componentWillMount() {
this.props.getUsers();
};
getUsersfunction(users){
return(
console.log("i was called");
users.map((user, idx) => {
return (
<View key={idx}>
<Text style={styles.userNames}> ID: {user.id} - Name: {user.name} </Text>
</View>
)
});
)
};
render() {
console.log("USERS: ", this.props.users);
const { users } = this.props.users
return (
<View style={styles.container}>
<ScrollView>
{ users.length ? (
{this.getUsersfunction(users)} // <= Is this the correct way?
) : null }
</ScrollView>
</View>
);
}
}
// mapStatetoProps and mapDispatchToProps here.
export default connect( mapStateToProps, mapDispatchToProps ) ( MyComp );
错误是this 是保留字。 Console.log 将所有用户播种在一个数组中。我做错了什么?
【问题讨论】:
-
getUsersfunction目前没有返回任何东西。您可能需要添加一个 return 语句。 -
更新问题。这是我的错误。原始代码有回报。仍然没有返回任何内容。但是控制台日志
"i was called"
标签: reactjs react-native ecmascript-6 react-redux