Create ReactJS Project
Create new folder named LearnReactJSWithRealApps and select to this folder in Visual Studio Code
Open Terminal windows in Visual Studio Code and use commands below:
npm install create-react-app@latest
npx create-react-app myapp
Create New Component
Open App.js file in src folder and create new component as below:
import React, { useState } from 'react';
function App() {
let [age, setAge] = useState(20);
let [username, setUsername] = useState('abc');
let [price, setPrice] = useState(4.5);
let [status, setStatus] = useState(true);
const changeValue = () => {
setAge(40);
setUsername('def');
setPrice(88);
setStatus(false);
}
return (
<div>
Age: {age}
<br/>
Username: {username}
<br/>
Price: {price}
<br/>
Status: {status ? "Show" : "Hide"}
<br/>
<input type="button" value="Change State Values" onClick={changeValue} />
</div>
);
};
export default App;
Add Component to Entry File
Open index.js file in src folder and add new component to this file as below:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
serviceWorker.unregister();
Structure of Project
Run Application
In Terminal windows in Visual Studio Code and type: npm start,
program will open url http://localhost:3000 on browser
Output
Click Change State Values button to change values in state