React_-_The_Complete_Guide_2024 - Copy
React_-_The_Complete_Guide_2024 - Copy
2024:
source : https://coursehunter.net/course/udemy-react16-complete-guide?
lesson=2
notes:
React ⇒ is a JavaScript library for building user interfaces.
why to use it ?
⇒ to insure smooth user experiences and it uses JS in the browser to update
the page and the user interface without reloading the webpage.
document.querySelector('input').addEventListener('input', function(ev
const value = event.target.value;
if (value.length < 5) {
event.target.style.borderColor = 'red';
} else {
event.target.style.borderColor = 'green';
}
});
//using react
function MyForm() {
const [inputValue, setInputValue] = useState('');
return (
<input
type="text"
value={inputValue}
onChange={handleChange}
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
const list = document.getElementById('data-list');
data.forEach(item => {
const listItem = document.createElement('li');
listItem.textContent = item.name;
list.appendChild(listItem);
});
});
//using React
function DataList() {
const [data, setData] = useState([]);
useEffect(() => {
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => setData(data));
}, []);
return (
<ul>
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
document.querySelector('#button').addEventListener('click', function(
document.querySelector('#content').textContent = 'Button was clicke
});
//using React
function InteractiveComponent() {
const [message, setMessage] = useState('');
return (
<div>
<button onClick={handleClick}>Click Me</button>
<p>{message}</p>
</div>
);
}
in React ⇒ you define the target UI state(s) - not the steps to get there,
instead, React will figure out & perform the necessary steps. (Declarative ).
To start a react project in the browser URL bar just write react.new and it should
open a sandbox code environment for writing code. and that gives you an in-
browser development environment.
to create a react project locally in you’re VScode you need to install node.js ,
npm and vite
then you need to write in the terminal npm create vite@latest folder-name .
to once and (initially after creating a new project,) install all the local packages
necessary for your project use npm install .
keep npm run dev up and running whilst working on your code, Quit it anytime
via CTRL + C , restart it once you start working again.
<script src="js.script.js"></script>
the keyword defer insures that the JavaScript code will only be executed only
after the rest of the HTML document has been read and parsed.
the type = “module” attribute in the script tag makes sure that this JS file is treated
as a JavaScript module. which unlocks the import syntax where you can import
code from script A into script B.
when working with react you’ll almost never add these script tags to your
html file on your own because react project almost always use a build process
which as part of this process injects these scripts tags in the code for you.
what does build process mean ? ⇒ it means that the code you write is not the
code that gets executed (like this ) in the browser, instead, the code is
transformed before its handed off to the browser.
2. the code would not be optimized for production (e.g. not minified
[minification ⇒ names of variables or functions are shortened to
reduce the amount of JS code that is served to the user] ).
//export a var
export const variableName = "i am a variable";
//importing syntax
Data Types:
1. let
2. var
3. const
console.log(userMessage );
console.log(x);
console.log(y);
Operators:
// Operators
console.log(6 + 2);
console.log("text 1 " + " ,Text 2"); //string concatenation
console.log(6 - 2);
console.log(6 / 2);
console.log(6 * 2);
console.log(6 % 2);
console.log(6 ^ 2);
// comparison operators
}
//this works without the return statement
greetings("Dan");
greetings("Sam", "how u doin ?");
NOTE !!! ⇒ functions must only have ONE return statement at most.
Functions without “return ” implicitly return “undefined”.
objects are used to group values together with key-value pairs. and is
created by using {}
an array is a special type of object and are created using [] and contains
a list of values. and the idea is to have just values in a certain order which
can be accessed by their position in that list.
arrays can contain all sorts of values, for e.g. another array, objects, string
etc…
array Methods:
// you can use map to transform the type for e.g. from string to object
const objectArr = hobbies.map((item) => ({text: item}));
console.log(objectArr);
de-structuring :
instead of creating variables from an array like this:
console.log(firstName);
console.log(lastName);
const user = {
name: "Dan",
//with destructuring
const {name, age} = user;
Components: are reusable UI building blocks that we create and then combine
to create the interactive user interface.
any website can be broken down into smaller building blocks: Components, it
can therefore also be built by creating & combining such components.
the idea behind creating a component is that they wrap HTML and possibly
CSS code as well as any JavaScript logic that might be needed and together
those languages and code pieces then define and control a part of the UI.
3. Separation of Concerns: