23

React Navigation's introduction page suggests the use of the following destructuring assignment:

const { navigate } = this.props.navigation;

However, when I implemented React Navigation in my App, ESLint is complaining about this line describing these both errors:

'navigation' is missing in props validation (react/prop-types)

'navigation.navigation' is missing in props validation (react/prop-types)

Even though the app seems to be working as intended, how would it be possible to remove these error lines?

Community
  • 1
  • 1
Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44

5 Answers5

22

React.PropTypes has moved into the prop-types package since React v15.5 (see Typechecking with PropTypes).

It should be used instead of importing from react-native (the package can be added into the project via npm install --save prop-types or yarn add prop-types).

And the example code complying with "Component should be written as a pure function" rule as follows:

// In addition to other imports:
import PropTypes from 'prop-types';

const LoginScreen = ({ navigation }) => (
  <View>
    <Button
      title="Login"
      onPress={() => navigation.navigate('MainScreen')}
    />
  </View>
);

LoginScreen.propTypes = {
  navigation: PropTypes.shape({
    navigate: PropTypes.func.isRequired,
  }).isRequired,
};
Gürol Canbek
  • 1,096
  • 1
  • 14
  • 20
14

Solution today (since object Proptype isn't accepted by default anymore):

export default class LoginScreen extends Component {
  static propTypes = {
    navigation: PropTypes.shape({
      navigate: PropTypes.func.isRequired,
    }).isRequired,
  }
}
mcabe
  • 260
  • 3
  • 15
13

One option is to add the propTypes prop to the component.

Example

LoginScreen.propTypes = {
  navigation: PropTypes.object.isRequired,
};

Another option is to disable eslint for that page and rule. More info here

Rule Options

This rule can take one argument to ignore some specific props during validation.

...
"react/prop-types": [<enabled>, { ignore: <ignore>, customValidators: <customValidator> }]
...
bennygenel
  • 23,896
  • 6
  • 65
  • 78
  • 3
    Newest version now complains about the object type: `Eslint: Prop type 'object' is forbidden react/forbid-prop-types` – mcabe Feb 26 '18 at 10:31
  • 13
    To clarify the rule option mentioned above it would look something like this: `"react/prop-types": ["error", { "ignore": ["navigation"] }]` – MikaelHalen Mar 20 '18 at 09:33
6

When Project needs navigation to almost all componenets. We can also silence the linting for that specific prop.

By adding the following in the eslint configuration:

    "rules": {
     "react/prop-types": ["error", { "ignore": ["navigation"] }]
}
Athul Raj
  • 349
  • 3
  • 11
1

In case of navigation in ES5 use something like this:

LoginScreen.propTypes = {
  navigation: PropTypes.object.isRequired,
};

in ES6 use this:

static PropTypes = {
  navigation: PropTypes.object.isRequired,
};

and

import PropTypes from 'prop-types';

karolkarp
  • 271
  • 2
  • 7
  • It's working but after adding these part of code another warning appear. `Caution: `PropTypes` also has a named export `object`. Check if you meant to write `import {object} from 'prop-types'` instead.` Do you know how to avoid this without throwing exception to `eslint`? – Mikołaj Wittbrodt Apr 08 '22 at 12:46