I am trying to learn prop-types and cause a warning intentionally but it is not working.
App.jsx:
import { PropTypes } from 'prop-types';
const App = (props) => {
return (
<div>
{props.name}
</div>
)
}
App.propTypes = {
name: PropTypes.string.isRequired,
}
export default App
main.jsx:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App name={1} />
</StrictMode>,
)
I am saying a string is required but passing in a number. If I dont pass anything to name ( <App />
) then there is no warning even though I have ‘isRequired’. I checked my browser console and I do have it set so i can see warnings. If i remove the import line in App.jsx, then I get an error saying PropTypes isnt defined. Whatever is passed to name is rendered on screen and if nothing is passed then the empty div is rendered.
package.json:
{
"name": "react-prop-types",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"prop-types": "^15.8.1",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@eslint/js": "^9.21.0",
"@types/react": "^19.0.10",
"@types/react-dom": "^19.0.4",
"@vitejs/plugin-react": "^4.3.4",
"eslint": "^9.21.0",
"eslint-plugin-react-hooks": "^5.1.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^15.15.0",
"vite": "^6.2.0"
}
}
node --version
v18.19.1
npm --version
9.2.0