React
REACT
JSX (JavaScript XML) is an extension to JavaScript syntax used in React. It allows you to write HTML-like code within JavaScript, making it easier to create and manipulate the structure of user interfaces.
JS:
1 | import React from "react"; |
HTML:
1 | <!DOCTYPE html> |
tags like image or link are self closing
JSX
JSX is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript.
It’s typically used with React
1 |
|
- ReactDOM.render() is the entry point into the React application. It takes two arguments:
- The React element or component that you want to render.
- The DOM element where you want to render the React component.
- in JSX, each expression must have a single outermost parent element. This means that when you’re writing JSX, you need to wrap your JSX elements in a single parent element. For example:
1 | // This is valid JSX |
expression vs. statement
Expressions give you values, while statements do something or control how your code runs.
Expressions in JavaScript produce values. They can be simple, like variables or numbers, or more complex, involving operators or function calls.
Statements in JavaScript perform actions or control the flow of execution. They don’t produce values and consist of keywords, operators, and expressions.
ES6: template literal
**{} (Curly Braces in JSX): **
- Used within JSX syntax in React or other libraries/frameworks that utilize JSX.
- Used to embed JavaScript expressions or variables within JSX elements.
- Allows dynamic values or JavaScript code to be included inside JSX elements.
Examples:
1
2const name = "John";
const greeting = <h1>Hello, {name}!</h1>;${} (Template Literal Syntax):
- Used in regular JavaScript outside of JSX syntax.
- Used specifically within template literals (or template strings).
- Allows for string interpolation, enabling the embedding of expressions or variables within a string.
1 | codeconst name = "John"; |
In the above code, the variable name
is interpolated within the template literal using ${}
. The value of name
is inserted into the resulting string.
To summarize, {}
is used in JSX to embed JavaScript expressions within JSX elements, whereas ${}
is used in regular JavaScript as part of template literals to interpolate expressions or variables within strings
1 | import React from "react"; |
HTML Global Attributes
1 | <h1 className="heading" contentEditable="true"> |
contentEditable is a global attribute
JSX Attributes & Styling React Elements
https://codesandbox.io/s/jsx-attributes-and-styling-kkycr?fontsize=14
inline styling:
https://codesandbox.io/s/inline-styling-in-jsx-159y7?fontsize=14
1 | ReactDOM.render( |
1 | const customStyle = { |
https://codesandbox.io/s/react-styling-practice-xpn8u?fontsize=14 (time greeting application)
react components
- how to structure your code and make it look cleaner?
- split off coding into a separate file:
https://codesandbox.io/s/react-components-j66pt?fontsize=14
If Time is a function and you want to export the result of calling that function, you should include parentheses to call it, like Time(). If Time is a variable holding a value, you don’t need parentheses.
1 | //notice: capitalize the first character |
Import , Export, Module
index.js:
1 | import React from "react"; |
math.js:
1 | // export multiple things at once |
DevTools
Map function
1 | import React from "react"; |
Key property
In React, when you use map
to render a list of components, it’s essential to assign a unique identifier to each component. This unique identifier is known as a “key.” The purpose of the key is to help React identify which items have changed, are added, or are removed from the list. React uses these keys to optimize rendering performance and to efficiently update the DOM.
1 | function App() { |
but you cant use key in card function:
card.jsx:
1 | import React from "react"; |
app.jsx:
1 | import React from "react"; |
React Hooks
- Only call Hooks at the top level: Don’t call Hooks inside loops, conditions, or nested functions. Always use Hooks at the top level of your React functional component or custom Hook. This ensures that Hooks are called in the same order every time a component renders.
- Only call Hooks from React functions: You can’t call Hooks from regular JavaScript functions. Hooks should only be called from within React function components or custom Hooks.
https://codesandbox.io/p/sandbox/usestate-hook-completed-ylxqj
what’s hook?
In React, a Hook is a special function that allows you to “hook into” React state and lifecycle features from functional components. Before the introduction of Hooks in React 16.8, state and lifecycle methods were only available in class components. With Hooks, you can use state and other React features in functional components without the need for classes.
Hooks are functions provided by React that you can use to add state and other React features to your functional components. They allow you to reuse stateful logic across multiple components without changing the component hierarchy.
Some commonly used React Hooks include:
useState
: Allows functional components to use state.useEffect
: Allows functional components to perform side effects (such as data fetching, DOM manipulation) after the component renders.useContext
: Allows functional components to consume context provided by aContext.Provider
higher up in the component tree.useReducer
: An alternative touseState
for managing more complex state logic with reducers.useCallback
anduseMemo
: Optimization Hooks for performance optimization by memoizing functions or values to prevent unnecessary re-renders.useRef
: Allows functional components to persist values across renders without causing re-renders.