
const functionOne = function() { alert('Hello world!'); }; const functionTwo = function() { alert('Hello world!'); }; functionOne === functionTwo; // false const functionThree = function() { alert('Hello world!'); }; const functionFour = functionThree; functionThree === functionFour; // true true . const object1 = {}; const object2 = {}; const object3 = object1; object1 === object2; // false object1 === object3; // true object1 = {} , this results in filling with some data a section of memory allocated specifically for object1 .object1 in the form of an address at which data structures related to an object are located in memory. Running the object2 = {} command results in the allocation of another memory area specifically designed for object2 . Are obect1 and object2 in the same memory area? No, each of them is allocated its own section. That is why when we try to compare object1 and object2 we get false . These objects may have an identical structure, but the addresses in the memory where they are located are different, and when comparing, the addresses are checked.object3 = object1 , we write the object3 address of object1 into the constant object1 . This is not a new object. This constant is assigned the address of an existing object. You can check it like this: const object1 = { x: true }; const object3 = object1; object3.x = false; object1.x; // false object1 constant. Then in the object3 constant the same address is written. Changing object3 leads to a change in the object in memory. This means that when accessing an object using any other link to it, for example, the one that is stored in object1 , we will work with its modified version.render function displays will not change either. Obviously, if the component remains the same, it does not need to be re-rendered. If nothing changes, the render function will return the same as before, so there is no need to execute it. This mechanism makes React fast. Something is displayed only when necessary.== operator. React does not perform "shallow" or "deep" comparison of objects in order to determine their equality. A “shallow comparison” is a concept used to describe the comparison of each key-value pair of an object, as opposed to a comparison, in which only the addresses of objects in memory (references to them) are compared. With a “deep comparison” of objects go even further, and if the value of the object properties being compared are also objects, the comparison of the key-value pairs of these objects is also performed. This process is repeated for all objects nested in other objects. React does nothing of the kind, performing only a check for equality of links.{ x: 1 } to another object that looks exactly the same, React will re-render the component, since these objects are in different memory areas. If you recall the above example, then if you change the property of a component from object1 to object3 , React will not re-render such a component, since the object1 and object3 refer to the same object. class SomeComponent extends React.PureComponent { get instructions() { if (this.props.do) { return 'Click the button: '; } return 'Do NOT click the button: '; } render() { return ( <div> {this.instructions} <Button onClick={() => alert('!')} /> </div> ); } } do ( do={true} or do={false} ) SomeComponent component.SomeComponent component is re-rendered (when the value of the do property changes from true to false and vice versa), the Button element is rendered again. The onClick handler, although it is always the same, is created anew each time the render function is called. As a result, it turns out that every time a component is created in memory, a new function is created, since its creation is performed in the render function, the link to the new address in memory is passed to <Button /> , and the Button component is also rendered again, despite the fact that nothing has changed at all.this context), then you can define it outside the component. All instances of the component will use the same function reference, since in all cases it will be the same function. Here's what it looks like: const createAlertBox = () => alert('!'); class SomeComponent extends React.PureComponent { get instructions() { if (this.props.do) { return 'Click the button: '; } return 'Do NOT click the button: '; } render() { return ( <div> {this.instructions} <Button onClick={createAlertBox} /> </div> ); } } createAlertBox , each time render called, will contain the same reference to the same area in memory. As a result, Button will not be Button again.Button component is small and quickly rendered, the above-described problem associated with the internal declaration of functions can also be found in large, complex components that take a long time to complete. This can significantly slow down the React application. In this regard, it makes sense to follow the recommendations, according to which such functions should never be declared inside the render method. class SomeComponent extends React.PureComponent { createAlertBox = () => { alert(this.props.message); }; get instructions() { if (this.props.do) { return 'Click the button: '; } return 'Do NOT click the button: '; } render() { return ( <div> {this.instructions} <Button onClick={this.createAlertBox} /> </div> ); } } SomeComponent when you press a button, various messages will be displayed. The event handler for a Button element must be unique to SomeComponent . When passing the cteateAlertBox method, cteateAlertBox doesn’t matter whether cteateAlertBox will be re-rendered. It doesn't matter if the message property has changed. The address of the createAlertBox function createAlertBox not change, which means that the Button element should not be re-rendered. This allows you to save system resources and improve the rendering speed of the application.map array method is used in the render method: class SomeComponent extends React.PureComponent { render() { return ( <ul> {this.props.list.map(listItem => <li key={listItem.text}> <Button onClick={() => alert(listItem.text)} /> </li> )} </ul> ); } } SomeComponent , it is unknown what these functions will be. How to solve this puzzle? class SomeComponent extends React.PureComponent { // SomeComponent // . clickHandlers = {}; // // . getClickHandler(key) { // , . if (!Object.prototype.hasOwnProperty.call(this.clickHandlers, key)) { this.clickHandlers[key] = () => alert(key); } return this.clickHandlers[key]; } render() { return ( <ul> {this.props.list.map(listItem => <li key={listItem.text}> <Button onClick={this.getClickHandler(listItem.text)} /> </li> )} </ul> ); } } getClickHandler method. This method, when first called with a certain value, will create a function unique to this value, put it in the cache and return it. All subsequent calls to this method with passing it the same value will result in it simply returning the function reference from the cache.SomeComponent will not re-render the Button . Similarly, adding items to the list property will result in the dynamic creation of event handlers for each button.key property for each JSX object resulting from the map method.[ 'soda', 'pizza' ] , and then turned into [ 'pizza' ] , and you cached event handlers with the command like listeners[0] = () => alert('soda') , you will find that when a user clicks on a button to which a handler is assigned with ID 0, and which, according to the contents of the [ 'pizza' ] array, should display the message pizza , the message soda will be displayed. For the same reason, it is not recommended to use array indices as properties that are keys.
Source: https://habr.com/ru/post/426053/
All Articles