反应本机 lint 严重失败?

react native lint critical failed?

运行ning npm 运行 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

我不会将 dispatch 传递给组件。我认为最好创建一个执行您需要的操作并通过 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);