Components
A piece of the puzzle
Last updated
A piece of the puzzle
Last updated
You frequently want to share code, just like in regular JavaScript, which you do by using functions. If you want to distribute JSX, you can do so as well. These functions are referred to as "components" in React and have some unique properties that's defined or called props.
To be more specific, components are functions that tell React how to render one type of element. Each function takes a props
object, and returns a new element that will be rendered in place of the original.
So what type of props
can a component receive?
Like HTML elements, they can certainly receive strings and numbers. Custom components can also receive objects, arrays, functions — or even other elements!
If we want to write JavaScript inside of JSX we need to wrap it with curly braces {} .we call that Interpolation
Taking template literals as an example :
Anything inside `` will be considered in string context but if we wrap it by ${} we can write JavaScript inside it and that's the JavaScript context . that's literally Interpolation in action .
Same thing inside JSX but we just ditch the $ and use curly braces and babel will understand that we are writing JavaScript there.
Bear in mind that you can't do statements inside of the curly braces as babel can't interpret them so the best why to do it is to wrap it inside a function
Expression: Something which evaluates to a value. Example: (1 + 5) / 2 Statement: A line of code which does something. Example: if Somthing does something
For an explanation of the important differences in composability (chainability) of expressions vs statements, my favorite reference is John Backus's Turing award paper, Can programming be liberated from the von Neumann style?.
Imperative languages (C, Java, etc.) emphasize statements for program structure and have expressions as an afterthought. Expressions are prioritized in functional languages.
Because pure functional languages have such powerful expressions, statements can be omitted entirely.
But you can use ternary operator because like you thought, it returns an expression
Now let's return to our initial code
Remember React.createElement():
As we said, the type argument for React.createElement() can accept various things one of which is a React Element .
In our case, we want to create a React Element representing the message function.
That will work just fine as react instead of creating a DOM node. It creates an element that is saved in memory and that will call our message function when we render it on the page.
If you can recall we can convert React.createElement() to JSX. Then our code will look something like this:
Babel compiler will not recognize message as a React Element. Instead, it will try to find it as a HTML Tags without a result so it will throw an error.
If you want learn more about HTML tags :
So we need to let babel know that's a React Element, and for that, all our React component's name will need to start with Uppercase letter.
We can even nest those element inside each others:
From here on, i'll will be dealing with JSX in the next chapters as it's easier to write and is more readable especially if we have a complex tree of components.
But always bear in mind that anything that we will do with JSX we can do with React raw APIs.
You can decide to conditionally evaluate one JSX expression over another using different techniques.
You can use the if...else statement .
In some cases, rather than using the if keyword , it is more convenient and more concise to use an on-the-fly condition (Inline If) with the && operator.
When using React, it's a common pattern to return multiple elements by wrapping them with a container element like div. This works fine, but by doing so, we're adding an extra node to the DOM. As your app grows, these extra nodes contribute to slow performance. Below is an example without fragments:
As you see in the dashboard component, we wrap the child elements with a div and then call the Paper component in the Home component. Now, let's see what it looks like in the browser.
As you can see in the example, we have an unused div that doesn't contribute to anything in the layout. This is redundant.
To solve this problem, Fragments were introduced in React. Fragments are used to group a list of children without adding extra nodes to the DOM. Let's look at an example where we use Fragments:
Now, if we look at the browser we'll see that no additional elements have been added to the DOM.
Fragments can be used using two types of syntax. The first one is using React.Fragment which we've used multiple times above. The other one is a new shorter syntax, using empty brackets, that does the same thing.
Both of these types of syntax work the same. Although, the newer syntax doesn't support keys or attributes.
OPTIONAL