Understanding Function References and Lazy Evaluation in React
Let’s break down the following code in detail for a beginner to React:
|
|
export default function App() {...}
: Here, we’re defining a function calledApp
. This is the main component of our application. It’s the highest level component, the parent of all other components. Theexport default
part means that we are making this function the default export of this module, so it can be imported into another module using an import statement.return (...);
: This is what theApp
function (and thus our component) returns when it is rendered. The stuff inside the parentheses is JSX, a syntax extension for JavaScript that allows us to write HTML-like code inside our JavaScript. In this case, it’s returning aToolbar
component with certain props.<Toolbar onPlayMovie={() => alert('Playing!')} onUploadImage={() => alert('Uploading!')} />
: Here we’re rendering aToolbar
component. We are also passing two props to theToolbar
component:onPlayMovie
andonUploadImage
. Each of these props is a function that displays an alert when called.function Toolbar({ onPlayMovie, onUploadImage }) {...}
: This defines a new component calledToolbar
. The curly braces{}
in the parameter of the function are used for destructuring, which allows us to extract multiple properties from an object in a single statement. ThisToolbar
component takes two props,onPlayMovie
andonUploadImage
, which were passed to it by theApp
component.<Button onClick={onPlayMovie}>...<Button onClick={onUploadImage}>...
: Here we’re rendering twoButton
components and passing them each anonClick
prop. The value of theonClick
prop is a function that we received from the parentApp
component. When the button is clicked, it will execute the corresponding function, showing an alert.function Button({ onClick, children }) {...}
: This is theButton
component definition. It takes two props,onClick
andchildren
. TheonClick
is a function that will be executed when the button is clicked. Thechildren
prop is a special prop in React, which is used to display whatever you include between the opening and closing tags when invoking a component.<button onClick={onClick}>
: Here we have a standard HTML button element. We’re assigning theonClick
prop that we received from the parentToolbar
component to theonClick
attribute of the button element. When this button is clicked, it will execute the function that was passed to it from theToolbar
component.{children}
: This is where we render thechildren
prop. Whatever is passed as children to theButton
component when it’s used will appear here.
To summarise, this is a simple React application with three components (App
, Toolbar
, Button
) where clicking on buttons in the Toolbar
triggers alerts as defined in the parent App
component.
Lazy Evaluation
The function onClick is a reference to a event handler. The value of the onClick argument could be either onPlayMovie or onUploadImage. These methods: onPlayMovie and onUploadImage when they are passed in to the Button component using { onPlayMove } is basically a reference to the function. So it does not get executed immediately. It is delayed execution based on the onClick event on the Button component. Because of functional nature of JavaScript we are able to pass functions as arguments to methods.
In JavaScript, functions are first-class citizens, which means you can do with them what you would do with other types such as strings or numbers. You can store them in variables, pass them around as arguments, return them from other functions, and so on.
In the case of the onClick
prop in your React component, you’re passing a reference to a function (either onPlayMovie
or onUploadImage
) to be executed when a click event occurs on that component. These functions are not invoked immediately when the component is rendered; instead, they are only invoked in response to a click event. This pattern is called “higher-order functions” and it’s a cornerstone of functional programming, a paradigm which JavaScript supports.
This kind of delayed execution is sometimes referred to as “lazy evaluation”, as the evaluation of the function is delayed until it’s actually needed. It’s a powerful technique that can help you write more efficient and flexible code.
React makes heavy use of this functional nature of JavaScript. It allows you to create components (which are essentially functions) and use them to build complex user interfaces with interactivity built in, like responding to user events with functions passed as props.