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 [result1, setResult1] = useState('');
let [result2, setResult2] = useState('');
let [result3, setResult3] = useState('');
let [result4, setResult4] = useState('');
let hello = (e) => {
setResult1('Hello ' + e.target.value);
}
let selectCategory = (e) => {
setResult2(e.target.value + ' is selected');
}
let selectFile = (e) => {
var selectedFile = e.target.files[0];
let result = 'File name: ' + selectedFile.name;
result += '<br>File type: ' + selectedFile.type;
result += '<br>File size: ' + selectedFile.size;
setResult3(result);
}
let selectFiles = (e) => {
let selectedFiles = e.target.files;
let result = '';
for (var i = 0; i < selectedFiles.length; i++) {
result += '<br>File name: ' + selectedFiles[i].name;
result += '<br>File type: ' + selectedFiles[i].type;
result += '<br>File size: ' + selectedFiles[i].size;
result += '<br>--------------------------';
}
setResult4(result);
}
return (
<div>
<fieldset>
<legend>Demo 1</legend>
<input type="text" onChange={hello} placeholder="Input your name" />
<br />
{result1}
</fieldset>
<fieldset>
<legend>Demo 2</legend>
Category <select onChange={selectCategory}>
<option value="c1">Category 1</option>
<option value="c2">Category 2</option>
<option value="c3">Category 3</option>
<option value="c4">Category 4</option>
</select>
<br />
{result2}
</fieldset>
<fieldset>
<legend>Demo 3</legend>
Photo <input type="file" onChange={selectFile} />
<br />
<span dangerouslySetInnerHTML={{ __html: result3 }}></span>
</fieldset>
<fieldset>
<legend>Demo 4</legend>
Photos <input type="file" onChange={selectFiles} multiple={true} />
<br />
<span dangerouslySetInnerHTML={{ __html: result4 }}></span>
</fieldset>
</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