Reputation: 431
I had an error related with the use of the prop-types library in a running React 16 application after update the node modules.
I created a new React 16 create-react-app project to check this issue and the error occur in the same way.
My code:
index.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App name="Test prop-types"/>,
document.getElementById('root')
);
App.js file:
import React from 'react';
import PropTypes from 'prop-types';
function App(props)
{
let name: PropTypes.string;
name = (props.name)
? props.name
: "Xxx";
return (
<div className="App">
<header className="App-header">
<p>Learn React - {name}</p>
</header>
</div>
);
}
export default App;
The error:
Line 8:23: Parsing error: Unexpected reserved type string
6 | function App(props)
7 | {
> 8 | let name: PropTypes.string
My package.json content:
{
"name": "test-prop-types",
"version": "0.1.0",
"private": true,
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Doc to use prop-types.
Upvotes: 3
Views: 1752
Reputation: 5576
You're defining proptypes in the wrong way. Proptypes are defined outside the component definition:
// This is wrong
function App(props)
{
let name: PropTypes.string
Correct way of setting proptypes:
import React from 'react';
import PropTypes from 'prop-types';
function App(props)
{
let name = (props.name)
? props.name
: "Xxx";
return (
<div className="App">
<header className="App-header">
<p>Learn React - {name}</p>
</header>
</div>
);
}
// Setting the proptypes of the `App` component
App.propTypes = {
name: PropTypes.string
};
export default App;
Upvotes: 2