React Concept: Purity

In JavaScript, purity refers to the concept of a function or component not causing side effects and always returning the same output for the same input. A pure function is one that does not mutate objects but only returns a new object. It takes an input and produces an output without changing the input or any other state. When called multiple times with the same input, a pure function will always produce the same output....

React DOM Events on Components

In this blog post, we’ll explore how to handle DOM events on React components and specifically address the challenge of showing or hiding a panel based on mouse hover status. Let’s say we have a link, and we want to show a panel when the user hovers over it. We can achieve this by using the onMouseEnter event on the <a> element that triggers the panel to show up. Here’s an example:...

React Higher Order Components: Creating Reusable and Composable Code

In the world of JavaScript programming, you might be familiar with Higher Order Functions that accept functions as arguments and/or return functions. React takes this concept and applies it to components, resulting in Higher Order Components (HOCs). HOCs are components that accept a component as input and return a component as output. The beauty of HOCs is their ability to make your code more composable, reusable, and encapsulated. They allow you to add methods or properties to the state of a component, or even integrate with Redux stores....

React Props: How to Pass Data Between Components

In React, props are a way to pass data from a parent component to its child components. This allows you to share data and information throughout your application. In this blog post, I will explain how to use props in both function and class components. Using Props in Function Components In a function component, props are passed as an argument to the function. You can access the props by adding props as the function argument....

React PropTypes: Enforcing Type Checking for Props

When working with JavaScript, enforcing the type of a variable at compile time can be challenging because of its dynamic nature. Passing invalid types can lead to runtime errors or unexpected behavior if the types are compatible but not what we expect. However, React offers a solution to this problem through PropTypes. With PropTypes, we can specify the required types for props, and our tools (such as editors and linters) can detect type errors before running the code....

React Render Props

Discover how Render Props can enhance your React application One common approach to share state between components in React is through the use of the children prop. In the JSX of a component, you can include {this.props.children}, which automatically injects any JSX passed in the parent component as children: class Parent extends React.Component { constructor(props) { super(props) this.state = { /*...*/ } } render() { return <div>{this.props.children}</div> } } const Children1 = () => {} const Children2 = () => {} const App = () => ( <Parent> <Children1 /> <Children2 /> </Parent> ) However, there’s a limitation here: the state of the parent component cannot be accessed from the children....

React: How to Make Text Editable on Double Click

In this blog post, we will explore how to implement the functionality of making a specific part of a page editable when double-clicked using React. To achieve this, we can utilize the toggle state variable to toggle between showing the editable element and the non-editable element. When the element is double-clicked, we change the value of the toggle state to display the appropriate element. Here is an example of how this can be implemented:...

React: How to Pass Props to Child Components

In React, it is common to have a hierarchy of components where props are passed from a top component to its children. In situations where you want to pass all the props from the parent component to its children without altering them, there is a simpler and more flexible approach. Instead of manually passing each prop, you can use the spread operator, which allows you to pass all the props to the child component in a concise and error-free manner....

React: How to Toggle Display of Different Components on Click

In React, it is common to want to display a different component when a button or link is clicked. This can be useful in scenarios like navigation menus, where you want to show different sections of your application based on user interactions. In this blog, we will explore how to achieve this functionality. To begin with, let’s create a simple button component called AddTripButton, which will trigger the display of a different component when clicked:...

React: Presentational vs Container Components

When working with React components, it is common to divide them into two categories: presentational components and container components. Presentational components are primarily responsible for generating the markup to be rendered. They do not manage any state, except for state related to the presentation itself. On the other hand, container components are mainly concerned with the “backend” operations. They handle the state of various sub-components, wrap multiple presentational components, and may interact with libraries such as Redux....