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 from 'react';
function App() {
let hello = () => {
alert('Hello PMK');
}
let hi = (e) => {
alert(e.target.value + ' button is clicked');
}
let changeBackgroundColor = e => {
document.bgColor = e.target.value;
}
return (
<div>
<input type="button" value="Hello" onClick={hello} />
<br/>
<input type="button" value="Hi" onClick={hi} />
<br/>
<input type="button" value="Red" onClick={changeBackgroundColor} />
<input type="button" value="Green" onClick={changeBackgroundColor} />
<input type="button" value="Blue" onClick={changeBackgroundColor} />
</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 buttons to call click events