Skip to content

React.PropTypes.string deprecated #22

Description

@cakeant

Hi shiningjason,
感謝你的教學, 很清楚明瞭,
我在學習過程中發現一點問題, 回報一下希望能幫助到你更版

“Level 7. 培養好習慣,設計防呆的 React 元件”章節有用到React.PropTypes.string
但React v15.5後已經不支援React.PropTypes, 進行到這段會報錯
(教學中提供的react cdn連結 https://cdnjs.com/libraries/react 目前已經是v16)

// 1. 使用 propTypes 定義參數的型別
TodoHeader.propTypes = {
  title: React.PropTypes.string,      //Error: Cannot read property 'string' of undefined
  username: React.PropTypes.string,
  todoCount: React.PropTypes.number
};

我上網爬了一下, 目前須改用prop-types
https://reactjs.org/docs/typechecking-with-proptypes.html
index.html: include prop-types

...
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.6.0/prop-types.min.js"></script>
...

TodoHeader.js: React.PropTypes改成PropTypes

// 1. 使用 propTypes 定義參數的型別
TodoHeader.propTypes = {
  title: PropTypes.string,
  username: PropTypes.string,
  todoCount: PropTypes.number
};```

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions