React how to update component when new input is added - reactjs

I have a react component in which a user can add teams and then players to these teams. I use react fetch for posting and getting the data from the database. My question is, how can I automatically update the component to show the newly added teams by the user? Currently I have to manually refresh to see changes.
Do I have to add another function to the button onClick to refresh the whole page or is there a better method in react to just refresh the component?
My render looks like this:
render() {
return (
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="jumbotron text-center">
<form>
<label for="teamName">Team name</label>
<input type="text" class="form-control" id="teamName"></input>
<button type="submit" onClick={this.addTeams} class="btn btn-primary">Submit</button>
</form>
<br></br>
Registered teams:
<br></br>
{
this.state.data.map((dynamicData, key) =>
<div class="black" id="panels">
<PanelGroup accordion id="accordion"
activeKey={this.state.activeKey}
onSelect={this.handleSelect}>
<Panel bsStyle="primary" eventKey={dynamicData.id}>
<Panel.Heading>
<Panel.Title toggle onClick={() => this.showPlayers(key)}>{dynamicData.name} <i class="fas fa-angle-down"></i></Panel.Title>
</Panel.Heading>
<Panel.Body collapsible>Add players</Panel.Body>
<Panel.Body collapsible>
<div id="dynamicInput">
<input type="text" class="bottomroom" placeholder="Player name" id={"playerName"+key}></input>
<input type="text" class="bottomroom" placeholder="Player number" id={"pnr"+key}></input>
<input type="hidden" class="bottomroom" id={"teamId"+key} value={dynamicData.id}></input>
{this.state.players.map((playersData, key) =>
<p>{playersData.name}</p>
)}
</div>
<br></br>
<button type="submit" onClick={() => this.addPlayers(key)} class="btn btn-success button"><i class="fas fa-check-circle"></i></button>
</Panel.Body>
</Panel>
</PanelGroup>
</div>
)
}
</div>
</div>
</div>
</div>
);
}

Related

How can I put the correct ID in my model?

I have an API where I want to put a template for each book in order to edit it on the same page.
Unfortunately, it always shows me the last data in the list and not the data I want.
What I actually want is when I press the PUT button for "id 2", I want my model to show me the data for "id 2" and not the last id from the database (for example, "id 10").
Here is my constructor and my call from my api :
constructor(props) {
super(props)
this.state = {
data: [],
openPost: false,
openPut: false
}
}
componentDidMount() {
axios.get('https://127.0.0.1:8000/api/books')
.then(response => {
this.setState({
data: response.data
})
})
}
Here is my code:
{
data.map(book =>
<div key={book.id}>
<div className="card ml-auto mr-auto mb-3">
<div className="card-body" key={book.id}>
<h2 className="card-title">{book.title}</h2>
<h6 className="card-subtitle mb-2 text-muted">{book.subtitle}</h6>
<p className="card-text">{book.content}</p>
<Model id={"Model_put-" + book.id} value={book.id} trigger={this.state.openPut} setTrigger={!this.state.openPut} id_book={book.id}>
<button onClick={() => this.setState({ openPut: false })} className="close-btn btn-danger">X</button>
<h3>My Model</h3>
<p>This is my button triggered Model</p>
<input type="text" name="id" className='form-control' value={book.id} ></input>
<div className="row">
<div className="col">
<input type="text" name="title" className='form-control' placeholder='Title' ></input>
</div>
<div className="col">
<input type="text" name="content" className='form-control' placeholder="Content" ></input>
</div>
</div>
<div className="col">
<textarea className='form-control mt-2' defaultValue={book.content} />
</div>
</Model>
<button onClick={() => this.setState({ openPut: true })} type="button" className="btn btn-warning" id={"book-" + book.id} href="#">
PUT
</button>
</div>
</div>
</div>)
}
And the contents of my Modal.js file:
return (this.props.trigger) ? (
<div className="modal">
<div className="modal-inner">
{ this.props.children }
</div>
</div>
) : ""
Thanks in advance !
Every one one your modals is looking at the same openPut variable, which is a boolean, so they are either all open, or all closed. It just so happens that the last one is the only visible one. Quickest fix is:
<Model
id={"Model_put-" + book.id}
value={book.id}
trigger={this.state.openPut === book.id}
setTrigger={!this.state.openPut} id_book={book.id}
>
/* ... */
</Model>
<button
onClick={() => this.setState({ openPut: book.id })}
type="button"
className="btn btn-warning"
id={"book-" + book.id}
href="#"
>
PUT
</button>

How do I use a row of custom buttons in a form tag instead of a select drop down menu?

I have a form component which filters data based on the name and category of an asset (ignore the name). I have used the tag to create a dropdown menu but I want to replace this with an array of custom buttons on a seperate row which filter the data when clicked to give assets of that category. Below is the relevant code I have used.
<div className="row">
<div className="col">
<div className="content-block">
Reset filter
<h3>
Filter
</h3>
<form onSubmit={ (e) => this.props.onTriggerFilter(e) } autoComplete="off">
<div className="form-row">
<div className="form-group col-md-3">
<label htmlFor="name">Name</label>
<input
type="text"
className="form-control"
id="name"
value={this.props.filterData.name}
placeholder="Eg, PDF asset..."
onChange={({target}) => this.props.onFilterInputChange('name', target.value)}
/>
</div>
<div className="form-group col-md-3">
<label htmlFor="supplier">Category</label>
<select
name="asset_type"
className="form-control"
value={this.props.filterData.asset_type}
onChange={({target}) => this.props.onFilterInputChange('asset_type', target.value, 'select')}
>
<option value="">All</option>
{ this.props.assetTypeOptions.map((o, key) => (
<option key={ key } value={ o.value }>{ o.label }</option>
))}
</select>
</div>
</div>
<button type="submit" style={{visibility:'hidden'}}>Submit</button>
</form>
</div>
</div>
</div>
I want to replace the select field with a row of buttons in the form
<button type="button" class="btn btn-primary btn-rounded waves-effect">Generic</button>
and get the data to filter when one is pressed. How do I do that?

Facing issue while tried to created login form in react

Input is a void element tag and must neither have children nor use dangerouslySetInnerHTML.
<div>
<Modal show={this.state.show} handleClose={this.hideModal} >
<div className="blkOverlay">
{/* This is Login Form to log in to your profile */}
<div className="formContent modal-main">n>
<h2>Welcome Back <span>Brandon!</span></h2>
<form show={this.state.show} handleClose={this.hideModal}>
<input type="text" name="email" placeholder="Email Address" />
<input name="password" type="text" placeholder="Password" passwordToggle="true" iconShow="eye" iconHide="eye-blocked" />
<div className="passContent">
<div className="checkingPass">
<input name="checkbox" type="checkbox"></input>
<p className="passFont">Remember Password</p>
</div>
<p className="passFont">Remember Password</p>
</div>
<input type="button" name="button">Login</input>
<div className="social-media-button">
<input type="button" name="button">Sign in with Facebook</input>
<input type="button" name="button">Sign in with Google</input>
</div>
<p className="passFont">Don't have an account? <span>Create a account</span></p>
</form>
</div>
{/* This is Sign up to create a account */}
</div>
</Modal>
</div>
What is the proper way to create a form in reactjs with getting this error about the input?
You should not put "Sign in with Facebook/Google" inside the input-tag. This is the reason why it is complaining about children. In React, a child is another tag inside a tag.
You should use:
<input type="button" value="Sign in with Facebook">

Validate an input field in HTML having 2 buttons. Validate only for one button click

This is the HTML file. There is no used in this. There are two buttons in this HTML. Using angularJS2( typescript )
<div class="visitor-entry form-group">
<!-- [ngClass]="{'validate':vName.errors && (vName.dirty || vName.touched)}"-->
<div class="ui-input-group">
<input #vName="ngModel" type="text" class="form-control" placeholder="Name*" name="visitorName" [(ngModel)]="visitorName">
<span class="input-bar"></span>
</div>
<div class="ui-input-group">
<input type="text" class="form-control" placeholder="Mobile" name="visitorMob" [(ngModel)]="visitorMob">
<span class="input-bar"></span>
</div>
<div class="add-btn-cont">
<button [disabled]="vName.errors" class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor();"></button>
</div>
</div>
<div class="block search-add-list active">
<h6>Person Affected
<button class="icon-btn pull-right addNewBtn" title="Add New" data-toggle="modal" id="personAffectedBtn" (click)="addNewPerson(personAffectedCode)></button>
</h6>
</div>
Here I want to validate the visitorName field mandatory, and add the class [ngClass]="{'validate':vName.errors && (vName.dirty || vName.touched)}. . THis should be validated only for first button click.
But if I add required to visitorName field then it is validating for both the button clicks.
How can I solve this issue.
You can set the value of required on button click
<div class="visitor-entry form-group" >
<!-- [ngClass]="{'validate':vName.errors && (vName.dirty || vName.touched)}"-->
<div class="ui-input-group" >
<input #vName="ngModel" type="text" class="form-control" placeholder="Name*" name="visitorName" [(ngModel)]="visitorName" [ngClass]="{'validate':vName.errors && (vName.dirty || vName.touched)}" [required]="{{reqVal}}">
<span class="input-bar"></span>
</div>
<div class="ui-input-group">
<input type="text" class="form-control" placeholder="Mobile" name="visitorMob" [(ngModel)]="visitorMob">
<span class="input-bar"></span>
</div>
<div class="add-btn-cont">
<button [disabled]="vName.errors" class="icon-btn" id="addVisitor" title="Add" (click)="addVisitor();reqVal=false"></button>
</div>
</div>
<div class="block search-add-list active">
<h6>Person Affected
<button class="icon-btn pull-right addNewBtn" title="Add New" data-toggle="modal" id="personAffectedBtn" (click)="addNewPerson(personAffectedCode);reqVal=true"></button>
</h6>
</div>

Show Mobile component ReactJS

import React, { Component } from 'react';
class BigText extends Component {
render() {
return (
<div>
<div>
<h1 className="row px-2">Big Text Notification</h1>
<hr />
<div className="row px-1 py-2 animated fadeIn">
<div className="px-1" style={{ width:50 + '%' }}><br />
<div className="mb-1">
<input type="text"
className="form-control"
placeholder="Title"
name="title"
/>
</div>
<div className="mb-1">
<textarea
className="form-control"
placeholder="Text"
name="text"
/>
</div>
<div className="mb-1">
<textarea
className="form-control"
placeholder="Summary"
name="summary"
/>
</div>
<br />
<div className="row px-2" >
<div>
<button className="nav-link btn btn-block btn-info" onClick={this.handleClick} >Save</button>
</div>
<div className="px-1">
<button className="nav-link btn btn-block btn-danger"> Cancel</button>
</div>
</div><br />
</div>
</div>
</div>
</div>
)
}
}
export default BigText;
i had added the code snippet
What I'm doing is I'm trying to add a Mobile frame as component on side of the fields so that whatever i write in the fields will be shown in that mobile being shown as the component on the screen
so i wanted to know how to add that mobile and how to show text in it as well

Resources