Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing PropTypes via the main React package is deprecated

I'm using redux but when I run my code I have this error:

Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.

I install

npm i prop-types -S

but I I still have the same error.

./components/action/article.js

import * as ArticleActionTypes   from '../actiontypes/article';  export const AddArticle = (name, description, prix, image) => {     return {         type: ArticleActionTypes.ADD_ARTICLE,         name,          description,          prix,         image     } }  export const RemoveArticle = index => {     return {         type: ArticleActionTypes.REMOVE_ARTICLE,         index     } } 

./components/actiontypes/article.js

export const ADD_ARTICLE = 'article/ADD_ARTICLE'; export const REMOVE_ARTICLE = 'article/REMOVE_ARTICLE'; export const UPDATE_ARTICLE = 'article/UPDATE_ARTICLE'; 

./components/reducers/article.js

import * as ArticleActionTypes   from '../actiontypes/article';  const initialState = [     {         name: 'test',         description: 'test',         prix: 'test',         image: 'url'     },     {         name: 'test',         description: 'test',         prix: test,         image: 'url'     } ]  export default function Article (state=initialState, action){     switch(action.type){         case ArticleActionTypes.ADD_ARTICLE :              return [                 ...state,                 {                     name: action.name,                     description: action.description,                     prix: action.prix,                     image: action.image                 }             ];         case ArticleActionTypes.REMOVE_ARTICLE :             return [                 ...state.slice(0, action.index),                 ...state.slice(action.index +1)             ] ;         default: return state;     } } 

index.js

import React            from 'react'; import { render }       from 'react-dom'; import {Provider}       from 'react-redux'; import {createStore}    from 'redux';  import ArticleReducer   from './components/reducers/article'; import Scoreboard       from './components/containers/Scoreboard';  const store = createStore(     ArticleReducer,     window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() )  render(<Provider>             <Scoreboard store={store}/>         </Provider>, document.getElementById('root')); 

./components/containers/Scorboard.js

import React                            from 'react'; import {connect}                        from 'react-redux'; import {bindActionCreactors}            from 'redux'; import PropTypes                        from 'prop-types';  class Scoreboard extends React.Component {      render(){         return (             <div>               Scoreboard             </div>         )     } }  const mapStateToProps = state => {     {         articles :state      } }  Scoreboard.propTypes = {   articles: PropTypes.array.isRequired }  export default connect(mapStateToProps)(Scoreboard); 
like image 932
Nedjim DN Avatar asked Apr 09 '17 06:04

Nedjim DN


People also ask

Is PropTypes deprecated?

PropTypes is deprecated since React 15.5.

Is PropTypes included in React?

PropTypes is React's internal mechanism for adding type checking to components. React components use a special property named propTypes to set up type checking. When props are passed to a React component, they are checked against the type definitions configured in the propTypes property.

What is the purpose of the PropTypes package?

PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.

Why we use PropTypes in react native?

react-native Props PropTypes The prop-types package allows you to add runtime type checking to your component that ensures the types of the props passed to the component are correct. For instance, if you don't pass a name or isYummy prop to the component below it will throw an error in development mode.


1 Answers

As others have mentioned- if you have audited your own project for PropTypes uses but you're still seeing the warning- it's likely coming from an upstream dependency. One way you can track down the cause of this warning is by setting a debug breakpoint where it's logged and then stepping back to the caller. Here's a quick example recording I made:

enter image description here

(A higher-quality version is available here.)

Once you've identified the library in question, consider filing a Github issue (or better yet- a PR!) to inform the authors of the new warning.

In the meanwhile, this is only a dev-mode warning so it should not affect production usage of your application.

like image 117
bvaughn Avatar answered Sep 29 '22 23:09

bvaughn