Building Dynamic Forms with React.js and Bootstrap for Easy Input Field Management

In the process of developing a web application using React.js and react-bootstrap, I encountered an interesting challenge. On one of the form pages, users should be able to input symptoms of an illness they are dealing with. The key functionality required is for users to type in text while also having the option to remove any previously entered text.

A helpful example showcasing this behavior in Javascript can be found at:

I aim to implement a similar feature using React framework. Here is what I have so far, but I am seeking guidance on how to proceed:

var closeButton = <Button onClick={this.removeSymptomField()}>Close</Button>;

<Input type="text" buttonAfter={closeButton} placeholder="Type a symptom here."/>

<Button onClick={this.addSymptomField()}>Click to add a new symptom.</Button>

Upon invoking this.addSymptomField(), my objective is to append a new Input field to the page. Conversely, triggering this.removeSymptomField() should result in removing the current Input field from view.

Your insights and advice would be greatly appreciated!

Answer №1

To easily manage current inputs, you can maintain a list in the component's state and update it with each call to addSymptomField and removeSymptomField

Initialize the input list in the component constructor like so:

this.state = { inputs: [] }

In the render method:

<div className="inputs">
   {this.renderInputs()}
</div>

Your renderInputs method can be implemented as:

renderInputs() {
   return this.state.inputs.map((input, index) => <Input key={index} type="text" buttonAfter={closeButton} placeholder="Type a symptom here."/>)
}

Simply add or remove inputs from the list by invoking the addSymptomField and removeSymptomField methods.

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

Controlling dropdown menus filled with AJAX responseData

When it comes to Javascript, JQuery has always been a reliable companion for me. However, there are instances where I encounter challenges that require some extra effort to overcome. Today happens to be one of those days. I've stumbled upon an issue t ...

Is it impossible to style an element within a div tag?

How can I apply styling to a specific div with id="1", which contains class="match"? Here is the code: <div class="col-lg-3"> <div id="1"> <p class="match">Match {this.state.matches[0].id}</p> <div class="tea ...

Expo is having trouble locating the module "color-convert" - such a frustrating problem

Having an issue! I ran the command npm start in my terminal and received the following error: Starting project at /home/pc/Documents/Projects/Mobile/weather_app Developer tools running on http://localhost:19002 Cannot find module 'color-convert' ...

Checkbox with an indeterminate state in Angular

I'm currently working on updating an AngularJS (1.5) setup where a parent checkbox becomes indeterminate if one of its children is selected, and all the children are selected if the parent is selected. My main challenge lies in converting the old ES5 ...

There seems to be a problem retrieving the JSON file

My JavaScript file is designed to fetch a JSON file and execute a function if successful. If there's an error, it should display an alert window with the message "error". However, despite checking the syntax, I keep receiving an error alert every time ...

Troubleshooting Problems with jQuery's .remove() Function

I attempted to create a simple game using JS and jQuery, keeping in mind that I am new to both languages. My goal was to create a function that allows users to delete their save within the game. However, despite my efforts, I faced issues with the function ...

Include a Class into a label using jQuery

I am having an issue where I need to specifically add the error class to a <label> with the name attribute set to choice_16_0. However, when I try to achieve this using my code, it ends up changing all labels on the page to <label for="choice_16_0 ...

Securing a single component within a named view router in Vue.js

Within my routes configuration, I have a named view route set up: let routes = [ { name: "home", path: '/', components: { default: Home, project: ProjectIndex } } ] The goal is to ...

The complexity surrounding various versions of jQuery, the .noConflict method, and the jQuery migrate feature

I was tasked with making a large-scale website responsive, and decided to utilize Bootstrap as the framework. However, I encountered issues due to the jQuery version (v1.8.2) being used. In my development environment, I resolved this by including the follo ...

Having difficulty invoking a JavaScript function through PHP

My goal is to achieve the following: <html> <script type="text/javascript" src="jquery.js"></script> <a id="random">before</a> <script> function test(a) { document.getElementById('random').innerHTM ...

Error in three.js library: "Undefined variable 'v' causing a TypeError" while creating a custom geometry

I am attempting to create my own custom geometry using three.js. However, I encounter an error when trying to define it with the following code: geometry = new THREE.FreeWallGeometry( 3, 5 ); The error message "TypeError: v is undefined" originates from ...

Creating dynamic templates for table rows in AngularJS directives

Is it possible to dynamically load an AngularJS Directive templateUrl while working within a table? In my scenario, I have the following HTML structure where I am repeating a tr element with a fw-rule directive: <tbody> <tr ng-repeat="rule in ...

Creating a Dialog/Modal inside a Material UI Card: A Step-by-Step Guide

I am attempting to include a Dialog/Modal within a Card using Material UI. My desired outcome is for it to appear like this: https://i.stack.imgur.com/3YAli.png The issue arises when I try wrapping the <Card> component around the <Dialog> com ...

Is there a way to modify the color of the horizontal line within react-native-otp-inputs in the React Native platform?

Is there a way to customize the color of the horizontal line in react-native-otp-inputs for react native? I used react-native-otp-inputs to capture user OTP input, but now I want to change the color of the default black horizontal line to white. You can re ...

What is the best way to ensure consistent Box Overlay Height across multiple boxes?

Is there a way to ensure that overlay box height remains the same even if the text lines are not the same length? Below is a snippet of my code. If you have a solution using jQuery or JS, please provide it. Thank you for your time and effort in trying to h ...

The module 'NgAutoCompleteModule' was declared unexpectedly by the 'AppModule'. To fix this issue, make sure to add a @Pipe/@Directive/@Component annotation

Currently, I am attempting to integrate an autocomplete feature into my Angular 2/4+ project. Despite trying various libraries, none of them seem to be working correctly. Each one presents me with a similar error message: Unexpected module 'NgAutoCom ...

What is the process for setting up extra routes in an Express server that is configured to work with React-Router

Currently, my server is set up to return index.html for every app.use(/* API call that is made. When I try to create a new route using app.get('/info', it still returns the index.html file. I am looking for a way to add new routes without having ...

The status of the 'slider' may be 'undefined'

I'm really struggling to figure out how to resolve this issue, even though I suspect it's a simple fix that's eluding me... Here is the part of my code in TypeScript that's causing the error: const slideLeft = () => { cons ...

Enhancing Win8 apps with AppendTo/jquery-win8

Recently, I've been intrigued by the ToDoMVC samples and decided to try porting them into a Windows 8 JS app. I thought it would be as simple as copying and pasting the code while making sure to reference the necessary WinJS libraries. However, I soo ...

Node.js error: Headers cannot be set once they have been sent

I'm struggling with sending form data through express.js and encountering the following error: After establishing communication, it's not possible to modify headers. This is the current state of my code: app.post('/api/users/profile/:us ...