运行npm run lint:critical之后,它返回一个错误,就是这样:
61:26 error Unexpected 'this' no-invalid-this
该功能要求用户授予位置检测权限:
_askForPermission = async () => {
const { dispatch } = this.props;
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
dispatch($isLocationActive()).catch((error) => dispatch(Activity.$toast('failure', error.message)));
}
} catch (err) {
console.warn(err);
}
};
行const {dispatch} = this.props;是导致该错误的原因,我需要修复,因为它导致管道CI / CD失败
答案 0 :(得分:1)
我不会将分派传递给组件。我认为最好创建一个执行您需要的操作并将其通过mapDispatchToProps
传递到组件中的操作。您的组件可能看起来像这样:
import { toastError } from './actions'
class Foo extends Component {
render() {
_askForPermission = async () => {
const { myNewPropFromAction } = this.props;
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
myNewPropFromAction()
}
} catch (err) {
console.warn(err);
}
};
return (
<div>Hello World</div>
)
}
}
const mapDispatchToProps = dispatch => ({
myNewPropFromAction: toastError
});
export default ({}, mapDispatchToProps)(Foo);