34

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);
KARTHIKEYAN.A
  • 11,752
  • 4
  • 81
  • 93
Nedjim DN
  • 513
  • 2
  • 6
  • 12
  • try this one: https://www.npmjs.com/package/proptypes, install it by using `npm install proptypes`, i think u r using some other npm module. – Mayank Shukla Apr 09 '17 at 06:38
  • check this blog also:https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html – Mayank Shukla Apr 09 '17 at 06:41
  • @MayankShukla As I can see here https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html the package OP is using is correct – Shubham Khatri Apr 09 '17 at 07:48
  • maybe you have some library that uses prop types in the old way – niba Apr 09 '17 at 07:51
  • 1
    @niba is right, you may be using libraries that still use propTypes the old way. What you can do is to delete the node_modules folder and install a package `npm install -g npm-check-updates` Then run `npm-check-updates -u` and `npm install` This will install the latest versions of each package from you. If the warning still persists you can revert to a older version of react till the updates for each package are available – Shubham Khatri Apr 09 '17 at 08:13
  • In my case my react-dom version was still 15.4.x – zhy2002 May 06 '17 at 05:46

7 Answers7

63

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.

bvaughn
  • 12,062
  • 37
  • 43
  • 1
    Very nice. But I can i figure it out if i'm using webpack? al the sources are from 'bundle.js' in this case, and I can't locate the problematic module – Chen Apr 12 '17 at 09:15
  • Then you have to kind of poke around the source code at the point shown to you by stepping up in the call stack. Usually there's a hint (some public method name, or some hard-coded string) that can help you narrow it down. – bvaughn Apr 12 '17 at 15:03
  • FWIW, In the first warning's stack trace you can also see Broadcast at line 104 being the issue – Joe Seifi Apr 13 '17 at 01:49
  • Yes, I'm aware. This isn't always the case though- perhaps the code has been bundled already at this point. Setting a breakpoint gives you more context when inspecting the unfamiliar code. – bvaughn Apr 13 '17 at 16:27
  • Thanks for the great answer but this seems needlessly awkward :-( Is there really a good reason why the warning doesn't actually help pinpoint where the deprecated usage lives, like every other sane warning out there? – Adam Spiers Aug 31 '18 at 21:07
16

Since the release of React v15.5.0 React.PropTypes is deprecated and has moved to another library. You should use npm install prop-types and use PropTypes from there.

The code at ./components/containers/Scorboard.js looks perfectly fine, you probably have a React.PropTypes somewhere else in your code.

Another options is that some dependency that you are using is still using it the old way. You can try to update your dependencies but as this deprecation is quite new I doubt that every library had already released an update.

More details about the PropTypes deprecation here.

UPDATE

It seems like redux has updated it's dependencies to use React v15.5.0 and got rid of the deprecation warnings. It is fixed in v4 and v5 as well.

Relevant pull requests: #663 and #666

Yuval
  • 1,218
  • 2
  • 12
  • 30
8

I solved this warning this way:

Installed PropTypes

# npm install -S prop-types

Import PropTypes like this

import PropTypes from 'prop-types';

instead of doing this:

import { PropTypes } from 'react';

Community
  • 1
  • 1
enam
  • 952
  • 11
  • 11
2

Be sure not to use React.PropTypes, sample:

MyComponent.propTypes = {
    MyString: PropTypes.string.isRequired
}
gpbaculio
  • 3,649
  • 7
  • 31
  • 67
  • What's the issue here? Assuming you've imported `PropTypes` from the `prop-types` package and not from React, [code like this is specifically mentioned as being fine](https://github.com/facebook/prop-types#difference-from-reactproptypes-dont-call-validator-functions). – White Elephant Nov 01 '17 at 11:39
  • Thanks! I still have a warning after using the npm `prop-types` package, it turns out in the code someone used `React.PropTypes`. Replacing it by `PropTypes` (imported from the npm package) fixed the problem. – sonlexqt Dec 05 '17 at 10:05
2

Also be sure to import React properly. I had this:

import * as React from 'react';

And should be:

import React from 'react';

This fixed all my warnings.

Gerard Brull
  • 886
  • 1
  • 8
  • 22
0

enter image description here

Resolved this issue by simply running npm install prop-types

AKV
  • 161
  • 4
  • 12
0

You can able to fix using upgrade versions

npm install --save react@15.4.0 react-dom@15.4.0
KARTHIKEYAN.A
  • 11,752
  • 4
  • 81
  • 93