Tips for incorporating Bootstrap classes into a React project, setting the className attribute to an empty string

After setting up Bootstrap and Create-React-App using npm on my local machine, I proceeded to create a new React app. The first component I worked on was counter.jsx:

import React, { Component } from 'react';
 class Counter extends Component {    
  state = {    
    name: test    
  };   
render() {  
 return ( 
  <div>
    <span> className="badge badge-primary">{this.state.name)}</span>    
    <button>Increment</button>    
  </div>
 );
}

export default Counter;

However, instead of seeing badges, I encountered

className="badge badge-primary">test
on my site. I made sure to import both the Bootstrap CSS and the component in my index.js where the component is rendered:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.css';
import Counter from './components/counter';

ReactDOM.render(
 <React.StrictMode>
  <Counter />
 </React.StrictMode>,
 document.getElementById('root')
);

To address this issue, I researched online and attempted to configure my webpack.config.js file. However, the changes didn't have the desired effect, and I'm uncertain if I implemented the correct adjustments. Have any of you encountered a similar problem before?

Answer №1

You made a mistake with the span tag (it is closed twice)

Replace:

<span> className="badge badge-primary">

With:

<span className="badge badge-primary">


Additionally, you are closing a ) right after this.state.name, which was not opened before. Make sure this is intentional.

Replace:
{this.state.name)}

With:
{this.state.name}


Updated

Alright, you have other issues as well. Here is a final version with all the fixes: https://codesandbox.io/s/magical-sun-qtwep?file=/src/App.js:0-306

What I changed:

  • You forgot the final } to close your component (after closing the render() function)
  • The variable test was missing, so I replaced name:test with name:'test'

Everything should be working fine now =]

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

Using and accessing Ajax response across all routes in an application

I am developing a Node.js Express API application that requires several AJAX calls at the start of the application for global data access in all requests. At the beginning of my app.js file, I include: var users = require('./modules/users'); I ...

Enhance the appearance of a custom checkbox component in Angular

I developed a customized toggle switch for my application and integrated it into various sections. Recently, I decided to rework it as a component. However, I am encountering an issue where the toggle switch button does not update in the view (it remains t ...

Exploring the methods for retrieving and setting values with app.set() and app.get()

As I am granting access to pages using tools like connect-roles and loopback, a question arises regarding how I can retrieve the customer's role, read the session, and manage routes through connect-roles. For instance, when a client logs in, I retrie ...

I am puzzled by the behavior of a specialized React Native hook that causes the component using it to render twice in a row

Currently, I am diving into the world of custom Hooks in React-Native while utilizing AWS Amplify for my backend. In this process, I have come across the Auth.currentUserInfo method provided by AWS Amplify to fetch authenticated user information. The chall ...

Incorporating lazy loading for diverse content to enhance pagination

I'm currently using jpaginate for pagination on a large dataset, but I've noticed that all the content loads at once. Is there a jQuery plugin available that will only load the content for the current page? ...

Incorporating external CSS stylesheets into a custom LESS framework

Is there a reliable method for integrating a third-party CSS file obtained from an external source into my own LESS compile structure without making many changes to it? I anticipate that the external CSS file may be updated periodically, so I would prefer ...

Launch in a new window using JavaScript

Check out my interactive map here. Currently, when I click on different sections of the map, the target page opens in the same window. However, I would like it to open in a new window instead. <iframe src="http://bluewingholidays.com/map/map.html ...

Obtain information from JSON within a function

I am struggling with extracting data from a JSON file in my React project. Despite having all the required data, I am not getting any output. Can someone please assist me? The value of this.props.id is 2, and I am attempting to locate the corresponding da ...

Using Modernizr to determine the presence of nth-child in a test

I'm attempting to utilize modernizr in order to check for browser support of the :nth-child selector, but I am unsure of how to proceed. I came across this particular example on http://jsfiddle.net/laustdeleuran/3rEVe/ which tests for :last-child. How ...

display different vue component based on screen size

I am in search of a method to implement responsive components in Vue.js (Nuxt). I have developed this mix-in but encountering an error: export const mediaQuery = { data() { return { breakpoints: { sm: 576, md: 768, lg: ...

Chai spy does not recognize sinon stubbed functions when verifying function calls

I am working with two asynchronous functions that return bluebird promises: Async1: function() { return new Promise(function(resolve, reject) { execute(query) .then(function(resp) { resolve(resp); }) .catch(function(err) { ...

State of an array has been modified

After toggling the state of a checkbox from true to false, calling setState does not immediately reflect the update on the screen. Instead, another element with state must be interacted with in order to trigger a refresh and display the new value of the ch ...

Exploring the differences between JavaScript destructuring and assignment

In my code, I initially used this line: const availableDays = status.availableDays; However, a suggestion was made to replace it with this line: const { availableDays } = status; Both options achieve the same result in one line of code, but I am curious ...

The blur() function in -webkit-filter does not seem to function properly in Vue.js

I'm having trouble implementing the -webkit-filter: blur() property. I tried using "filter" instead, but it still isn't working. Could this be a limitation of Vue.js or are there any other solutions available? <template> <div class=&qu ...

What is the most effective way to incorporate the DOMContentloaded event listener into the document using nextJS?

I am working on integrating an HTML code snippet into my Next.js project. The snippet includes an external script with a createButton function. <div id="examplebtn"></div> <script type="text/javascript"> //<![ ...

The perplexing phenomena of Ajax jQuery errors

Hey there! I'm having a bit of trouble with ajax jquery and could use some guidance. $.ajax({ type:"get", url:"www.google.com", success: function(html) { alert("success"); }, error : function(request,status,error) { alert(st ...

What steps can a web developer take to view user console errors?

Is there an effective method for a web developer to receive notifications of console errors logged by other authorized users? I am currently working with Express, Passport, and MongoDB. These errors may occur on either the client or server side. One approa ...

Step-by-step guide on incorporating a new JSON object into an array to display its elements as components on a webpage

Could I adjust the state of an array by incorporating values from a data.js file as a starting point? The process involves executing the setAllThingsArray function to add a new element, determined by the setThingsArray function based on the previous state ...

Implementing Express 4: The Correct Way to Serve Routes from External Files

Something about my Express 4 application is causing me frustration. Here's the configuration: routes/profile.js var express = require('express'); var router = express.Router(); router.get('/profile', function(req, res) { res.s ...

Ways to display output on a single line using javascript

It seems like such a simple task, but no matter how I try, I cannot get proper spacing between my words. /s does not seem to work for me. Here is an example of what I have attempted: document.write('You have been alive' + */s userMsDays /s* + &ap ...