2026-08-12 22:05
Tags: Front End React Web Development Software Engineering Guide Learning Programming Concepts Programming Lanuages Frameworks
How to use React
Project Structure
See DFAR - Module 1 - Introduction to React and Class Components for how to create a react project
-
Generally, React would be a front end framework that you can use to develop web applications
-
The overall structure of it is that you have the main basic HTML
index.htmlfile which is pretty bare bones and empty, and really only holds things like the title of your webpage and then a body resembling:<body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body> -
Where this basically points you over to a
.jsxfile (which I assume to be some modification of a standard javascript file) -
This main. jsx file though is basically where you would create the root components of your application, which would lead you into the main
App.jsxfile as well (and is possibly where you also toss in things like the body/main website looking CSS)
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)-
After this though you basically get over to the main application, where you also import in the
./App.cssfile and I assume other css files that you would like to use as well. This is where you would write what I can assume to be both your actual html and javascript code for your application, though given imports, I think you can set up a more modular set up and important in different things with different functions, so basically have a:function someOtherElement() { //some random javascript like code that would handle the behavior for that element, like button clicks and shit return ( //HTML code that would define the structure of that element and how it would look ) } -
Which I assume can then be imported over to the main app function and someone connected to it? maybe via a call? I am not entirely sure how.
- this is how ^^^^
-
To get the actual React workspace ready and set up for you:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashwhich copies over the code needed (if not already installed)- Restart your terminal
- Then run
nvm install --ltsandnvm use --lts - Then to check it worked
node -v & npm -v
-
This should have you basically installing the code background code (similar to python maybe, like a virtual environment?), and let you use react because react is compiled and processed via node
-
Then to Create a Project:
npm create vite@latest <project name> -- --template reactwhich given the react and template feature makes me assume that vite can handle many more templates than the simple react one, but who knows- Then you can go ahead and cd into the project and use
npm installwhich is going to read apackage.jsonand download all of the dependencies into anode_modulesfolder
-
RUNNING the actual project
- Each time you want to actually run the project so you can see it live, you would have to move into the directory with the project code, and run
npm run devwhich will open the website for access at http://localhost:5173/
- Each time you want to actually run the project so you can see it live, you would have to move into the directory with the project code, and run
FROM CLAUDE STRUCTURE OVERVIEW
midascloud-portal/
src/
App.jsx ← your main component, edit this first
App.css ← styles for App.jsx
main.jsx ← entry point, mounts App into the HTML page (don't touch rlly)
index.css ← global styles
index.html- the actual HTML page (barely any content React injects into it)
package.json ← lists your dependencies and scripts
Variables
import { useState } from 'react'
import './App.css'
function App() {
const [username, setUsername] = useState(''); // changes, affects UI → state
const [password, setPassword] = useState(''); // same
const appName = "MidasCloud"; // never changes → plain const, no state needed
function handleSubmit() {
const trimmedUsername = username.trim(); // temporary local variable, not state
console.log(trimmedUsername);
}
return (
<div>
<h1>{appName}</h1>
<input value={username} onChange={(e) => setUsername(e.target.value)} />
</div>
);
}
export default App-
This basically dictates the way in which you would use Variables inside of a react program, react basically handles your variables in a way that lets them be used in an automatic manner to update the live website
-
This means you can declare a variable normally mostly if it will not change the website content, so good for things maybe like booleans and all that other kind of stuff for logical portions of the code
-
There is also however another weirder way which is
const [username, setUsername] = useState('');where basically, you define the variable there as username and then a function that is sort of used to indicate to react that the state of that variable has changed, so you call it after I assume changing the state of that variable in order to update the visual UI of that function, generally following a principle of[item, setItem]where the use state defines something that I can assume to be the initial value of the variable that you want to set
Basic Example
import { useState } from 'react'
import './App.css'
function App() {
const [draft, setDraft] = useState('');
const [username, setUsername] = useState('');
function handleSubmit() {
setUsername(draft.trim());
}
return (
<div>
<input value={draft} onChange={(e) => setDraft(e.target.value)} />
<button onClick={handleSubmit}>Submit</button>
<p>Hello, {username}, it is great to meet you!</p>
</div>
);
}
export default App
- This example basically shows a simple web app where you type in a user name and then it updates a hello message whenever you click a submit button
- This generally uses a sort of
draftmethod because the thing is that react would automatically update the state of an object that would exist inside of the input bar, in this case the draft var, which holds the data in memory, then on button click, that memory is switched over to the user name button. - But, if user was in there directly, it would update dynamically, showing a new name every single keystroke, which is good because it means you get automatic storage of your input, without have to do the whole
document.getelementbyId()shabang
UseEffect
What useEffect actually is, precisely
You’ve been putting all your logic either directly in the component body (runs every render) or inside event handlers like onClick (runs only when clicked). useEffect is the third category: code that should run in response to the component appearing on screen (or specific values changing), not in response to a user action.
useEffect(() => {
// this function runs after React renders the component
}, []); // this array controls WHEN it re-runsThat second argument — the dependency array — is the whole mechanic to understand:
[](empty array) — “the values this effect depends on never change” → React runs it exactly once, right after the component’s first render, and never again. That’s what you want here: start the typing animation once when the login page first loads, don’t restart it every timepasswordstate changes from typing (which, remember, re-runs your whole component body — butuseEffectis specifically exempt from re-running unless its dependencies change).- No array at all — runs after every render (rare, usually not what you want).
[someValue]— runs once initially, then again only whensomeValuechanges between renders.
