【问题标题】:react native lint critical failed?反应原生 lint 关键失败?
【发布时间】:2019-05-20 23:46:06
【问题描述】:

运行 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 失败

【问题讨论】:

    标签: react-native continuous-integration eslint lint npm-scripts


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2021-03-27
      • 2016-03-19
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      相关资源
      最近更新 更多