React createElement function does not include string children when there are multiple children present within the inner HTML

Currently, I am utilizing React.createElement(...) to dynamically produce a basic react website using data from a JSON file.

The JSON file consists of an array of JSON objects that represent elements and their respective children. An individual element's JSON object appears as follows:

{
    "id": "e960f0ad-b2c5-4b0b-9ae0-0f6dd19ca27d",
    "render": true,
    "component": "small",
    "styles":[],
    "classes":[],
    "children": [
        "©2017",
        {
            "id": "04fa3b1a-2fdd-4e55-9492-870d681187a4",
            "render": true,
            "component": "strong",
            "styles":[],
            "classes":[],
            "children": [
                "Awesome Company"
            ]
        },
        ", All Rights Reserved"
    ]
}

This particular object is expected to generate the following HTML code:

<small>©2017 <strong>Awesome Company</strong>, All Rights Reserved</small>

To facilitate this, I have developed React components for rendering these HTML elements. For example,

Small.jsx

import React from 'react'

const Small = ({ id, className, style, children }) => {
    return (
        <small id={id} style={style} className={className}>
            {children}
        </small>
    )
}

export default Small

Additionally, I have created JSX files for other HTML elements such as anchor tags, divs, footers, buttons, etc.

There exists a renderer.js module which is invoked by App.js to render each component specified in the JSON file.

import React from 'react'
import Button from "../components/Button";
import Input from "../components/Input";
import Header from "../components/header/Header";
import Footer from "../components/footer/Footer";
import Div from "../components/containers/Div";
import Article from "../components/containers/article/Article";
import Fragment from "../components/containers/Fragment";
import Anchor from "../components/Anchor";
import Nav from "../components/Nav";
import Heading1 from "../components/typography/Heading1";
import Strong from "../components/typography/Strong";
import Small from "../components/typography/Small";

// Code omitted for brevity

function renderer(config) {
    // Code implementation details
}

export default renderer;

Unfortunately, there is an issue where the text content within the <small> and <strong> tags is not being inserted properly, resulting in empty elements when rendered on the site.

The current output looks like this:

<small id="e960f0ad-b2c5-4b0b-9ae0-0f6dd19ca27d" class=""><strong id="04fa3b1a-2fdd-4e55-9492-870d681187a4" class=""></strong></small>

It is evident that the text has not been displayed within the elements as intended.

When only a simple string is provided as the "children" attribute without an array, it displays correctly. This behavior is similar for anchor tags as well.

Referring to the React documentation at https://reactjs.org/docs/react-api.html#createelement, the third parameter of createElement expects an array of children.

Even attempting to wrap the text within an empty fragment did not yield the desired outcome.

In light of this challenge, how can plain text be inserted within the inner HTML of anchor, small, strong tags alongside additional children?

Answer №1

This seems to be a straightforward issue, as it appears to primarily involve handling string nodes.

Kindly review the following condition:

typeof config.children === "string"

and update it to:

typeof child === "string"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for closing 2 models in a React application

Currently, I am working on a project using ReactJs. One of the functionalities I am implementing is generating a user's gallery when the user clicks on their avatar. To achieve this, I am utilizing the react-grid-gallery package along with semantic ui ...

Position the Material UI Box element to the bottom of its parent container

Hello there, I've come across the following layout: <Box width="100%" height="100%" p={1}> <Box my={1} display="flex" justifyContent="center"> </ ...

What is the process for generating an API endpoint in Next.js that includes a query string?

I am currently working on setting up an API endpoint in Next.js, specifically the following one: /api/products/[name]?keyword=${anykeyword}. To accomplish this, I understand that I have to create it within the pages/api/products/[name] directory in index ...

The state continues to refresh countless times, despite implementing useEffect

Currently, I am enrolled in a React course by Brad Traversy. However, the course is a bit outdated and I am implementing hooks and modern React concepts for my project. Despite this, I have encountered an issue. Whenever I navigate to the Detail page of a ...

Positioning of vertical linear gradients in CSS

Check out this live demonstration: http://jsfiddle.net/nnrgu/1/ I then adjusted the background position to 0px -70px and observed the changes here: http://jsfiddle.net/nnrgu/2/ The linear gradient seems to disappear! Am I doing something wrong, or is ...

Is it necessary to use state if I have a static list of items containing titles and descriptions that I want to utilize across several individual components?

These instructions are part of a process that involves the following steps: [ { title: 'Upload file', description: 'This step allows you to upload your file and ....'}, { title: 'Pick a color', description: 'You have ...

What could be the reason that the style tag present in this SVG is not impacting any of the elements?

My SVG file consists of a single element with a fill set to red: <svg width="100" height="60" viewBox="0 0 100 60" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill="red" d=&qu ...

Retrieve jQuery CSS styles from a JSON database

I've been attempting to pass CSS attributes into a jQuery method using data stored in a JSON database. However, it doesn't seem to be functioning as expected. I suspect that directly inputting the path to the JSON variable may not be the correct ...

Appending a new item to an array within a React useState object

As a JavaScript beginner, I am looking to add an element to an array within an object that is part of a react state. const [addVendor, setAddVendor] = React.useState({ vendorName: "", vendorPhone: "", vendorWhatsApp: " ...

How can I eliminate duplicate URLs from my NextJs sitemap generator?

Currently in my NextJs application, I'm utilizing the nextjs-sitemap-generator package for managing the sitemap. My challenge lies in preventing duplicate URLs from being included. How can I incorporate this into the ignoredPaths configuration? For e ...

Targeting props within a Class Component: A Step-by-Step Guide

I am currently working on a MultiStep Form project. On the final page of the form, my goal is to push all the collected data to Firebase. I have been utilizing props and states to pass values from one page to another. However, I'm facing an issue in t ...

Prevent users from selecting multiple rows in DataGrid using Material UI

I am trying to solve an issue with the Material UI DataGrid where I want to only allow selection of one row at a time in the checkbox section. Currently, when I select a checkbox, multiple rows get selected which is not the desired behavior. I have attempt ...

experiencing an excessive amount of rerenders when trying to utilize the

When I call the contacts function from the main return, everything seems fine. However, I encounter an error at this point: const showContacts = React.useCallback( (data: UsersQueryHookResult) => { if (data) { return ( < ...

I am experiencing an issue where the tooltip does not appear when I click the icon. What adjustments can be made to the code to ensure that the tooltip

I have created a feature to copy abbreviation definitions when the clipboard icon is clicked. A tooltip displaying 'Copied' should appear after clicking the icon, but for some reason, it's not visible. Here's the code: $(document).re ...

Testing the units of a system ensures that the flow and data storage are reliable

Currently, I'm facing an interesting challenge when it comes to unit testing and the flux data stores. Because the data stores are singletons that are only instantiated once (upon module import), any changes made during unit tests remain persistent. ...

The button is disabled once inline CSS is applied to it

I was having issues with the CSS of my buttons being overridden by the user agent stylesheet. To fix this, I used inline CSS to override it. However, a new problem emerged where the buttons became unclickable. Here is the code I am using. Any assistance wo ...

How can the style of a div's :after pseudo-element be modified when hovering over its :before pseudo-element?

Here is some CSS code: .myDiv:before{ content:''; width:15px; height:15px; display:block; } .myDiv:after{ ... ... display:none; } And here is the corresponding HTML code: <div class='myDiv'></div> ...

Customizing content based on Route - Utilizing Node.js/React

I am currently working on setting up routes for different pages of store profiles in my Node app. I have been doing some research online and have come to understand how to adjust the parameters, but I am struggling with figuring out how to dynamically chan ...

Reat is having trouble sending a POST request to a Spring Boot application

Encountering an issue: I have a Spring Boot API set up with various endpoints and a filter to determine access with or without a JSON Web Token. Postman works as expected, returning the correct data, but React seems to struggle with it. The root cause of t ...

What is the best way to structure forms using MaterialUI?

Trying to organize the layout of a form with MaterialUI Grid components and fields: +-----------------------------------------+ |Form | | | | +--------------+ +----------------+ ...