React is a Javascript front-end library that is used to build single-page applications (SPA). Knowing the version can help you understand the available features, check for updates, and ensure compatibility with other dependencies. In this article, we will explore different methods to check the version of ReactJS.
In this article, we are going to discuss three ways to find out the React version.
- Using package.json file
- Using command line
- Using the version property of default import from React
Using package.json file:
The package.json contains metadata about our project. It is created by default when we create our React project. We can create a react app using the command mentioned below.
npx create-react-app name_of_the_app
The package.json file contains a lot of information in the name/value pairs in JSON format. We can easily check our React version under the list of dependencies as shown in the image given below.
Using the command line:
We can easily check the React version by using the command mentioned below on our command line.
npm view react version
The output demonstrating the use of the above command on the command line is mentioned below.
Using the version property of default import from React:
The default import from React library is an object that has a version property on it. We can use this property inside our JSX elements in our desired manner.
Syntax: The syntax to use the version property is mentioned below.
import React from 'react'; let a = React.version
To follow along with the article create a react project using the command that was discussed above in the article.
Filename: App.js The content of the App.js file is mentioned in the code given below in which we have demonstrated the use of the version property on React object.
Javascript
import React from 'react' ; const App = () => { return <h1> We are currently using react version {React.version} </h1>; } export default App; |
Step to run the application: Use the following command on the command line to start the application.
npm start
Output: Open the browser and go to http://localhost:3000, you will see the following output.