Error in React Bootstrap: ReferenceError - 'openModal' is not defined

Whenever the button is clicked, my intention is for a modal to open. I have written the code for it, but I'm encountering errors that I can't seem to resolve. Here's the snippet of my code:

import React from "react";
import {
    Modal,
    Button,
    Card,
    CardHeader,
    CardBody,
    CardTitle,
    CardFooter,
    Table,
    Row,
    ButtonGroup,
    Col,
  } from "reactstrap";

  function Table() {
    state = {
      isOpen: false
    };
    openModal = () => this.setState({ isOpen: true });
    closeModal = () => this.setState({ isOpen: false });

  return (
      <>
        <div className="contentGroup">
          <Row>
            <Col md="12">
              <Card>
                    <CardHeader>
                    <ButtonGroup style={{float: 'right'}}>
                        <Button className="btn-round btn-icon" color="blue" onClick={this.openModal} style={{float: 'right'}}>
                            <i className="icon-plus-add"/>
                        </Button>
                    </ButtonGroup>
                   </CardHeader>
                       ...
             </Card>
             <Modal show={this.state.isOpen} onHide={this.closeModal}>
              <Modal.Header closeButton>
              <Modal.Title>Modal heading</Modal.Title>
              </Modal.Header>
              <Modal.Body>Modal text</Modal.Body>
              <Modal.Footer>
              <Button color="info" onClick={this.closeModal}>
                Close
              </Button>
              </Modal.Footer>
              </Modal>
          </Col>
        </Row>
     </div>
    </>
   );
  }

export default Table;

The console is displaying the following errors:

'state' is not defined       no-undef     
'openModal' is not defined   no-undef 
'closeModal' is not defined  no-undef

I am unsure why these errors are occurring. Any assistance would be greatly appreciated.

Answer №1

It is recommended to rename the component to CustomTable instead of using Table to prevent any naming conflicts with the imported Table from reactstrap.

Avoid using this keyword within functional components and consider implementing hooks in your code.

import React, { useState } from "react";
import {
    Modal,
    Button,
    Card,
    CardHeader,
    CardBody,
    CardTitle,
    CardFooter,
    Table,
    Row,
    ButtonGroup,
    Col,
  } from "reactstrap";

function CustomTable() {

  const [ isOpen , setIsOpen ] = useState(false);
    

  const openModal = () =>  setIsOpen(true);

  const closeModal = () => setIsOpen(false);

  return (
      <>
        <div className="contentGroup">
          <Row>
            <Col md="12">
              <Card>
                    <CardHeader>
                    <ButtonGroup style={{float: 'right'}}>
                        <Button className="btn-round btn-icon" color="blue" onClick={openModal} style={{float: 'right'}}>
                            <i className="icon-plus-add"/>
                        </Button>
                    </ButtonGroup>
                   </CardHeader>
                       ...
             </Card>
             <Modal show={isOpen} onHide={closeModal}>
              <Modal.Header closeButton>
              <Modal.Title>Modal heading</Modal.Title>
              </Modal.Header>
              <Modal.Body>Modal text</Modal.Body>
              <Modal.Footer>
              <Button color="info" onClick={closeModal}>
                Close
              </Button>
              </Modal.Footer>
              </Modal>
          </Col>
        </Row>
     </div>
    </>
   );
}

export default CustomTable;

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

Encountering a peculiar issue with including no-cors in the header while fetching JSON content in a React.js application

As someone who is relatively new to react and nodeJS, I am currently experimenting with pulling JSON data from my nodeJS webservices and displaying it in a react component. let url = "http://codepen.io/jobs.json"; let iterator = fetch(url); iterator ...

Using multiple ng-controller directives with the "controller as x" syntax on a single element

What is the reason that Angular does not allow two ng-controller directives on a single element and What are some potential solutions for this issue - such as using custom directives or nesting HTML elements with a single ng-controller directive, among oth ...

Am I on the right track with my service definition in Angular?

(function(){ angular.module('myApp',[]) })(); (function(){ angular.module('myApp.dashboard',[]) })(); (function(){ angular.module('myApp.value',[]) })(); (function(){ 'use strict'; angular.modu ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

Vue - Unable to navigate to a different route

I just started working with Vue and attempted to redirect '/home' to '/travel', but for some reason it's not functioning correctly. Can someone please guide me on how to achieve this? What could be the issue with my code? Thank y ...

Can a React Class Component utilize a Modal feature successfully?

I need help with rendering a single draft in my modal without redirecting to another component. I can display the draft, but it redirects me due to the Link element. However, if I remove the Link, I lose the draftId in the URL. Here's a snippet of my ...

Problem with setting up Rect JS

Just starting out with react js, I've successfully installed the stable version of node js. Verified the node version and npm version, everything seems to be working fine. However, when attempting to install react using "npm init react -app" & "npx cr ...

Using a data() object in Vue to fill out form fields efficiently

My data retrieval process from mongodb involves obtaining the data and transferring it to the client side using the following approach: error_reporting(E_ALL); ini_set('display_errors', '1'); require '../vendor/autoload.php'; ...

How about we display the Extents Algorithm?

Oh wise and knowledgeable coding community, I come seeking your expertise... I am working with the three.js library and facing a challenge in implementing a 'show extents' button. This button should adjust the camera position so that all objects ...

Show a button instead of text based on the current status

I have a dynamic table displaying various data, mostly text, but with a few buttons included. Now, I need to figure out how to handle this Here is the current data structure: [ { "BET": 57630343, "CUSTOMER": 181645, "XX_FILL OPEN": true }, { "BET": 57 ...

Tips for preserving input field data in an AngularJS scope variable

I am having trouble storing textbox values in my 'Values' variable. When I enter values in the textboxes, it seems to be getting the hard-coded values instead. Can someone please help me with this AngularJS issue as I am new to it? Here is the co ...

Trouble toggling Reactstrap navbar in my TypeScript project using NextJS

I recently integrated Reactstrap into my NextJS TypeScript project and encountered an issue with the Navbar component. Despite following the example from the Reactstrap documentation, the mobile toggle menu does not open when clicked. Additionally, none of ...

Using socket.io in a Django template without the need for the node.js service or socket.io.js file

I am working on a Django app that requires real-time push to clients. I have decided to use node.js and socket.io as it is considered the easiest platform for achieving this functionality. To implement this, I have included the socket.io framework code in ...

What is the best way to display the error message within a typography element?

When trying to display an error string between typography tags, I encountered a problem where the error was visible in the console but not rendered on the page. To troubleshoot, I attempted to declare a variable within the render method and called the han ...

Tips for resolving the error message: React is not able to identify the `currentSlide` and `slideCount` prop on a DOM element

I recently implemented an image slider using "react-slick" in my next.js project. However, I encountered some warnings in the console related to the 'currentSlide' and 'slideCount' props on a DOM element. Warning: React does not recogni ...

Ways to enlarge image size without compromising the image resolution?

My image is in PNG format or as a blob. It has dimensions of 800px by 600px. However, when I try to resize it using various canvas methods like the one mentioned in this Stack Overflow thread: Resize image, need a good library, it loses quality. I wou ...

Unlock the Power of Vue Draggable in Vue.js 3 with These Simple Steps

When attempting to implement Draggable in Vue.js 3, I encountered an error message: VueCompilerError: v-slot can only be used on components or <template> tags. Below is a snippet of my code: <draggable tag="transiton-group" name="s ...

How can I add a new property to an object type within an Interface in TypeScript?

I'm currently exploring how to merge declare an interface, with the twist of adding a property to the object literal type instead of directly to the interface itself. Within a library, I have a type that looks like this: interface DefaultSession { ...

Utilizing a form on numerous occasions prior to its submission

As a newcomer to JavaScript, I am exploring the best approach for a specific task. The task involves a form with checkboxes representing different music styles and a selector for names of people. The goal is to allow users to select music styles for mult ...

Error message: The Next.js GraphQL-Yoga server is returning a 404 error on the

I am brand new to using graphql-yoga 3 in conjunction with next js for creating APIs with GraphQL. Unfortunately, my routes at /api/graphql are returning a 404 error even though I have followed the example provided here. The default page loads fine, but I ...