Spreading activity among react elements

Recently, I decided to repurpose a login/register modal by incorporating a Modal to manage its visibility.

In the index.js file, I made the following changes:

const Form = ({ initialState = STATE_SIGN_UP, showPopUp = STATE_HIDE }) => {
  const [mode, toggleMode] = useToggle(initialState);
  const [display, toggleDisplay] = useToggleDisplay(showPopUp);

 
  return (
    <Modal className="modal" show={showPopUp} size="lg">
        <Container pose={mode === STATE_LOG_IN ? "signup" : "login"}>
        <div className="container__form container__form--one">
            <FormLogin mode={mode} />
        </div>
        <div className="container__form container__form--two">
            <FormSignup mode={mode} />
        </div>
        <Overlay toggleMode={toggleMode} mode={mode} />
        </Container>
    </Modal>
  );
};

In addition, I included a close icon in FormLogin along with an onClick event. To ensure functionality, I utilized a console.log for testing purposes. However, I am struggling to figure out how to close the modal from within the FormLogin class when the close action occurs.

I attempted to utilize the toggleDisplay function, but encountered difficulties. The implementation of toogleDisplay is as follows:


export const STATE_SHOW = true
export const STATE_HIDE = false

const useToggleDisplay = initialState => {
  const [display, setDisplay] = useState(initialState)
  const toggleDisplay = () =>
    setDisplay(display === STATE_SHOW ? STATE_HIDE : STATE_SHOW)
  return [display, toggleDisplay]
}

export default useToggleDisplay

Does anyone have any suggestions or solutions?

Answer №1

To activate the closing function, simply assign a handler named onClose (which is set to toggleMode in this case) to your modal and trigger it whenever the close button is pressed.

Answer №2

To close the modal, make sure to use the function toggleDisplay and pass it as a prop to your form component.

<FormLogin mode={mode} toggleDisplay={toggleDisplay} />

Within the FormLogin component, wherever you have the click handler for closing the modal, call toggleDisplay(false).

<button onClick={() => toggleDisplay(false)}>Close</button>

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

Modifying Bootstrap image dimensions

I've been working on creating a card div with two images inside: an up arrow and a down arrow. Everything looks good when the page is in full screen mode, with the images displaying at their full size. However, with Bootstrap's responsive design ...

Repairing a navbar that is leaning to the left

Currently working on my first front-end project, which is a website for a school club. I've been utilizing the Bootstrap library for CSS, but have encountered an issue with my navbar placement. It seems to be leaning more towards the left side rather ...

Implementing dynamic ng-forms with real-time validation

My directive includes a template with an ng-form: <ng-form name="autocompleteForm"> <div class="form-group" show-errors> <input type="text" class="form-control" ng-model="ctrl.val.value" name="autocompleteField" required> < ...

Hidden input for Vue form submission

I need to include two Id's with a user's input by adding them in a hidden input field. Since v-model doesn't work with hidden inputs, I have set up my hidden input like this: <input type="hidden" ref="property_id" :nam ...

Express.js: Defining a base route can cause issues with resolving static files

I'm currently working on a project using express.js and react.js, but I've encountered some issues that I can't seem to find solutions for. I have set up a base directory where the express server is located, and within that, there's a ...

Error: React is unable to read the property 'map' because it is undefined

Hey there! I recently started learning React and encountered an issue while following a course. The error message "TypeError: Cannot read property 'map' of undefined" popped up in my code. Can anyone help me figure out what's causing this pr ...

Express JS form validation using hapi/Joi fails to accurately validate the input data in forms

I am currently facing an issue with my form validation using the hapi/Joi package. The problem is that the schema keys are all taking the value of "undefined", which results in the first validation error being returned. How can I resolve this issue? Additi ...

Is it necessary to uncheck the row checkbox when inputs are left empty after blurred?

Two issues have cropped up here. When clicking on an input within a row, the corresponding checkbox should be checked. Currently, only the first text input is able to check the checkbox due to the use of .prev(). Is there a different approach that can be t ...

Retrieving the value of a submit button within the `onSubmit` event handler in a React application

In my usual process, I typically handle the form submission by utilizing the onSubmit handler. <form onSubmit={e => { ... } }> <input ... /> <input ... /> <button type="submit">Save</button> </form> ...

The clear icon in React will only appear when there is a value inputted

Is there a way to implement the clear X icon that only appears when typing in the input field using reactjs, similar to the search input on the Google homepage? Check it out here: https://www.google.com import { XIcon } from '@heroicons/react/solid&ap ...

What are the benefits of sticking with Express versus transitioning to a combination of JavaScript and HTML?

Recently, I've been developing a web application that involves taking user input to make modifications to files on the server side. The main logic for this project is built in node.js and operates via command line, with the rest of the site being deve ...

What is the best way to integrate a React component library that relies on Styled Components with another library that also has a dependency on Styled Components?

After recently converting my internal component library to Styled Components, I encountered an issue with duplicate instances of 'styled-components' in my Create React App (CRA) project. Both the component library and the CRA application are impo ...

What are the standard stacking order rules for elements in HTML?

Trying to understand the default HTML rules for element stacking order. Are there any restrictions regarding the order of parents and children or sibling elements in a single row? Or is it solely dependent on CSS? For clarification, consider this example ...

Is it possible to vary the font size for different fonts within a single page?

Imagine using Helvetica for English text and a unique, exotic font (from an ASCII perspective) for another language on the same page. Even with the same font size, one font may appear larger to the eye. Is it possible to specify different font sizes for ...

A Vue filtering feature that consistently adds 5 additional elements upon each use

I was wondering, how can I create a computed property filter function that always adds 5 more elements to the current array? Here are more details: Template: <span class="box-content" v-for="item in activeItems" :key="item.id"> <img class=" ...

Is passport.js necessary if I am implementing Auth0 for authentication in my React application?

While passport.js is commonly used for server side authentication, if I am already utilizing Auth0 as the authentication service for my React application, do I still require passport.js? ...

Adjusting the size of an element when hovering without causing any displacement of neighboring elements

I've been attempting to get this to work, but I'm running into some difficulties. Here's the example I'm currently working on: HTML: <ul> <li><img></li> <li><img></li> </ul> CSS ul{pos ...

Tips for transmitting form information in a fetch call

As I was developing a nodejs server, I encountered an issue with the POST call that involves sending form input data to a remote server. Despite everything else working fine, the form data was not being received by the server. Below is the code snippet in ...

Unable to add content to jQuery wrap

Why is it that you cannot append an element to the one used for wrapping another - as shown in the example below? var $test = $('.test'), $test1 = $('.test1'), $move = $('.move'), $testWrapper = $('<div ...

What is the correct way to adjust the style.top attribute of an element using JavaScript?

In order to correct a JavaScript source code, I need to adjust the style.top property of a div element based on an integer parameter from a function called index. Here is the current implementation: div.style.top = (index * 22 + 2)+"px"; However, for lar ...