import { usersFetch, onToggleFollow } from '../actions';
class Browse extends Component {
render() {
return (
<ListView
....
renderRow={(user) =>
<UserItem user={user}
onPress={() => this.props.onToggleFollow()}
/>}
/>
);
}
}
export default connect(mapStateToProps, { usersFetch, onToggleFollow })(Browse);
在子UserItem.js
中class UserItem extends Component {
render() {
followButton = <Button title='Follow' onPress={() => {
this.props.onToggleFollow();
}}/>;
return (
<View style={styles.cardContainerStyle}>
<View>
{ followButton }
</View>
</View>
);
}
}
在动作创建者文件中:
export const onToggleFollow = () => {
console.log('onToggleFollow method!');
return (dispatch) => {
dispatch({ type: ON_TOGGLE_SUCCESS });
};
};
我得到一个错误说:“this.props.onToggleFollow不是函数,this.props.onToggleFollow未定义”在UserItem.js中,任何帮助谢谢!
答案 0 :(得分:1)
您的问题就在这一行:
onPress={() => { this.props.onToggleFollow(); }}
UserItem
中的。您将该函数作为onPress
道具传递,因此您应该调用this.props.onPress
。