Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation 'navigation' is missing in props validation

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?

like image 839
Vinícius Figueiredo Avatar asked Oct 21 '17 21:10

Vinícius Figueiredo


People also ask

How do I fix missing in props validation?

To fix ESLint error missing in props validation with React, we can add a comment or disable the rule globally with a config. to disable prop type check for the line immediately below the comment in our code. in . eslintrc to disable the prop type validation rule for the whole project.

Is missing in props validation React JS?

To fix the 'React eslint error missing in props validation' when developing a React app, we can set the prop types of the props in the component causing the error. to import the prop-types package to let us add prop type validation to the Foo component.

Which type of validation is not provided by React Navigation?

React Navigation does not include support for the peek & pop feature available on devices with 3D touch.

What is the type of Navigation in react native TypeScript?

React Navigation library is one of the most used navigation libraries in the React Native ecosystem. It is written in TypeScript, and you can create React components and apply any navigation patterns from Stack, Tab, and Drawer.


Video Answer


4 Answers

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,
};
like image 195
Gürol Canbek Avatar answered Oct 17 '22 23:10

Gürol Canbek


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,
  }
}
like image 41
mcabe Avatar answered Oct 17 '22 22:10

mcabe


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> }]
...
like image 13
bennygenel Avatar answered Oct 17 '22 23:10

bennygenel


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"] }]
}
like image 5
Athul Raj Avatar answered Oct 18 '22 00:10

Athul Raj