34
loading...
This website collects cookies to deliver better user experience
A server-side rendered application enables pages to load faster, improving the user experience.
Search engines can easily index and crawl content because the content can be rendered before the page is loaded, which is ideal for SEO
Rendering server-side helps efficiently load webpages for users with slow internet connection or outdated devices.
Rendering static HTML from server-side is efficient, but for complex applications with frequent server requests and full page reloads can increase load times due to the bottleneck of the server performance. It can be very costly and pressuring the server.
Server-side rendering may not be compatible with third-party JavaScript code
Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event.
It provides better cross-browser compatibility as it provides a uniform api and consistent behavior on top level.
It provides better performance as for the native broswer events, the browser will create a new event object for the listener and bind it to the node. If we have multiple listener, multiple objects will be created and require a lot of resources from the memory.
Hooks are easier to work with and to test.
Code looks cleaner, easier to read.
Element: An element is a plain object describing a component instance or DOM node and its desired properties. It is a way to tell React what you want to see on the screen. They are not the actual instances.
Component: Component encapsulate element trees, but generally it can be deemed as a function which takes props as arguments and return a Element tree.
setState()
), causes the component and all its child components to be re-rendered automatically.setState()
leads to traversing the entire tree by default. (We can improve this by using memo hooks)key must be unique to each element in the array
do not use the index
do not use an unstable key such as random number
this.state
inside it. It also has no lifecycle so you shouldn't use those methods and hooks.const refContainer = useRef(initialValue);
useRef
returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
ReactDOM.createPortal(child, container)
getChildContext()
, the Context object returned by the component can access the Context properties from all its parent component.In most cases, we recommend using controlled components to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.
Since an uncontrolled component keeps the source of truth in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components. It can also be slightly less code if you want to be quick and dirty. Otherwise, you should usually use controlled components.