Home
React
๐ŸŒ React Fundamental: Custom components
May 05, 2025
2 min

Table Of Contents

01
๐Ÿ“Œ What Is a Component in React?
02
๐Ÿงฑ Building Simple Components with Functions
03
๐Ÿ› ๏ธ Introducing React's Raw API
04
๐Ÿ’ก Using JSX to Write Custom Components
05
๐Ÿงช JSX Naming Conventions Breakdown
06
๐Ÿงฎ Practicing with Props: A Calculator Component
07
๐Ÿง  Summary

๐ŸŒŸ Understanding Custom Components in React: A Beginner-Friendly Guide

One of the most powerful features of React is the ability to create custom components. They make your code reusable, more readable, and easier to maintain. If youโ€™re already familiar with JavaScript functions, youโ€™re halfway there. In this post, weโ€™ll explore how React components work and how to write them using both the raw API and JSX.


๐Ÿ“Œ What Is a Component in React?

In React, components are just functions.

Yes, you read that right!

A React component is a function that accepts an object called props and returns something that can be rendered.

Hereโ€™s a simple example:

function Greeting(props) {
return <h1>Hello, {props.name}</h1>
}

You can use this component just like a regular HTML tag in JSX:

<Greeting name="World" />

๐Ÿงฑ Building Simple Components with Functions

Before jumping fully into Reactโ€™s component model, letโ€™s start with a basic function that returns a React element. For instance, say you want to create the following HTML structure:

<div class="container">
<div class="message">Hello World</div>
<div class="message">Goodbye World</div>
</div>

You can define a message function like this:

function message(children) {
return <div className="message">{children}</div>
}

Then use it in JSX like so:

<div className="container">
{message('Hello World')}
{message('Goodbye World')}
</div>

This isnโ€™t the standard way to define React components, but it helps illustrate how functions can return JSX.


๐Ÿ› ๏ธ Introducing Reactโ€™s Raw API

Reactโ€™s createElement function is what JSX gets compiled into. Normally, you might see:

React.createElement('div', { className: 'message' }, 'Hello World')

But you can also pass a function as the first argument:

function Message(props) {
return <div className="message">{props.children}</div>
}
const element = React.createElement(Message, { children: 'Hello World' })

React will call Message with the provided props, and use the JSX it returns.


๐Ÿ’ก Using JSX to Write Custom Components

While using createElement is powerful, JSX is way more intuitive and readable. Letโ€™s take the same example and write it using JSX:

function Message(props) {
return <div className="message">{props.children}</div>
}
const element = <Message>Hello World</Message>

Much cleaner, right?

๐Ÿง  But wait โ€” why does this work?

JSX elements are transformed by Babel into React.createElement calls. But thereโ€™s a rule:

The name of the component must be capitalized for Babel to treat it as a custom component.

If you write <message>Hello</message>, Babel will interpret message as a string and generate:

React.createElement('message', {}, 'Hello')

But if you use <Message>, it becomes:

React.createElement(Message, {}, 'Hello')

So always capitalize your component names!


๐Ÿงช JSX Naming Conventions Breakdown

Hereโ€™s how Babel interprets JSX based on naming:

JSX SyntaxCompiled To
<Capitalized />createElement(Capitalized)
<property.access />createElement(property.access)
<lowercase />createElement('lowercase')
<kebab-case />createElement('kebab-case')
<Upper_Snake_Case />createElement(Upper_Snake_Case)

Avoid lowercase or kebab-case names for custom components, or Babel will think youโ€™re creating a standard DOM element!


๐Ÿงฎ Practicing with Props: A Calculator Component

Letโ€™s now build a more dynamic component: a Calculator that receives props and renders an expression with its solution.

function Calculator(props) {
const { left, operator, right } = props
let result
switch (operator) {
case '+':
result = left + right
break
case '-':
result = left - right
break
case '*':
result = left * right
break
case '/':
result = left / right
break
default:
result = 'Invalid operator'
}
return (
<div>
<code>
{left} {operator} {right} = <output>{result}</output>
</code>
</div>
)
}

Use it like this:

<Calculator left={1} operator="+" right={2} />

This would render:

<div>
<code>
1 + 2 = <output>3</output>
</code>
</div>

๐Ÿง  Summary

React components are just functions that return something renderable. To make them work seamlessly with JSX:

โœ… Capitalize your component names โœ… Pass props as the function argument โœ… Return valid JSX (or null, strings, numbers, etc.) โœ… Embrace reusability and readability

Reactโ€™s magic isnโ€™t so mysterious when you peel back the layers โ€” itโ€™s just functions, props, and return values. And thanks to JSX and Babel, we get to write expressive, intuitive code that compiles down to performant raw APIs.


๐Ÿ“š Further Reading

  • JavaScript in JSX with Curly Braces โ€“ React Docs
  • What is JSX? โ€“ Kent C. Dodds

Happy Reacting! โš›๏ธ๐Ÿ’ป


Let me know if youโ€™d like this turned into a downloadable blog post, formatted for MDX, or integrated into a website.


Tags

#ReactFundamental

Share

Related Posts

Introduction
๐Ÿš€ React Introduction
January 01, 2026
1 min
ยฉ 2025, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube