redux-form initialValues - reactjs

I kinda getting lost here while i try to populate a redux-form V6.
I'm trying to create a contact form with person and company details. Since I'm new to the react universe I jump from one Doc/Tutorial to the other... So it may looks a bit scraped together.
I don't understand the concept how I can tell the Form that it has
to submit a ID that is comming from "props.match.params.id".? Or is
it just a bind and will get the result when i call this.props.dispatch(getContact(this.props.match.params.id)); in the Contact Component?
The reducer returns a Object that has the Person and Company as
"sub"-Object how can i match that to the Form?
Is there a way to debug the Form? I just see that my request returned a contact but i don't know if it even reaches the Form.
I use react-router to link to:
/contacts/contact/:id -> Populated Contact Form
Because of the redux/routerv4 issues i have to wrap my React.Component in withRouter to get the params in props. I don't know how i can connect withRouter and mapDispatchToProps, So my Contact Component looks as follows:
import React from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { FormattedMessage } from 'react-intl';
import {getContact} from "../../../Actions/contactsActions";
import ContactForm from './Forms/ContactForm';
#connect((store) => {
return {
contacts: store.contacts,
countries: store.countries,
contactCategories: store.contactCategories
}
})
class Contact extends React.Component {
constructor(props) {
super(props);
this.state = {
errors: {},
};
}
componentWillMount() {
if(this.props.match.params.id) {
this.props.dispatch(getContact(this.props.match.params.id));
}
}
handleSubmit(data) {
console.log(data);
}
render() {
return (
<div className="contact-container">
<ContactForm onSubmit={this.handleSubmit} />
</div>
)
}
}
export default withRouter(Contact);
And the Form looks like:
import React from 'react'
import { connect } from 'react-redux'
import { Field, reduxForm, formValueSelector } from 'redux-form'
import { FormattedMessage } from 'react-intl';
import {getContact} from "../../../../Actions/contactsActions";
class ContactForm extends React.Component {
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit}>
<div className="tab-pane fade show active" id="general" role="tabpanel" aria-labelledby="general-tab">
<div className="row">
<div className="col-lg-6 col-6">
<h2>
<FormattedMessage id={ 'Company' } values={{name:"Company"}} />
</h2>
<div className="form-group row">
<label htmlFor="company-name" className="col-4 col-form-label">
<FormattedMessage id={ 'Companyname' } values={{name:"Company name"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="company-name" placeholder="Company name" />
</div>
</div>
<div className="form-group row">
<label htmlFor="other-name" className="col-4 col-form-label">
<FormattedMessage id={ 'Othername' } values={{name:"Other name"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="other-name" placeholder="In case of sister company/group"/>
</div>
</div>
</div>
<div className="col-lg-6 col-6">
<h2>
<FormattedMessage id={ 'Person ' } values={{name:"Person"}} />
</h2>
<div className="form-group row">
<label htmlFor="person-language" className="col-4 col-form-label">
<FormattedMessage id={ 'Language' } values={{name:"Language"}} />
</label>
<div className="col-8">
<Field component="select" className="form-control" name="person-language" />
</div>
</div>
<div className="form-group row">
<label htmlFor="person-gender" className="col-4 col-form-label">
<FormattedMessage id={ 'Gender' } values={{name:"Gender"}} />
</label>
<div className="col-8">
<Field component="select" className="form-control" name="person-gender">
</Field>
</div>
</div>
<div className="form-group row">
<label htmlFor="person-salutation" className="col-4 col-form-label">
<FormattedMessage id={ 'Salutation' } values={{name:"Salutation"}} />
</label>
<div className="col-8">
<Field component="select" className="form-control" name="person-salutation" />
</div>
</div>
<div className="form-group row">
<label htmlFor="person-title" className="col-4 col-form-label">
<FormattedMessage id={ 'Title' } values={{name:"Title"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="person-title" placeholder="Prof. Dr. Dr."/>
</div>
</div>
<div className="form-group row">
<label htmlFor="first-name" className="col-4 col-form-label">
<FormattedMessage id={ 'First-name' } values={{name:"First name"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="first-name" placeholder="First name"/>
</div>
</div>
<div className="form-group row">
<label htmlFor="further-names" className="col-4 col-form-label">
<FormattedMessage id={ 'Further-names' } values={{name:"Further names"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="further-names" placeholder="Further names"/>
</div>
</div>
<div className="form-group row">
<label htmlFor="last-name" className="col-4 col-form-label">
<FormattedMessage id={ 'Last-name' } values={{name:"Last name"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="last-name" placeholder="Last name"/>
</div>
</div>
<div className="form-group row">
<label htmlFor="job-title" className="col-4 col-form-label">
<FormattedMessage id={ 'Job-title' } values={{name:"Job title"}} />
</label>
<div className="col-8">
<Field component="input" className="form-control" name="job-title" placeholder="Last name"/>
</div>
</div>
</div>
</div>
</div>
</form>
)
}
}
const validate = (values) => {
const errors = {};
if(!values.Companyname) {
errors.Companyname = "Enter a Companyname";
}
return errors;
};
ContactForm = reduxForm({
validate,
form: 'contact',
enableReinitialize : true
})(ContactForm);
ContactForm = connect(
state => ({
initialValues: state.contacts.contacts
}),
{ load: getContact }
)(ContactForm);
export default ContactForm;

Related

EmailJs and Form validation control

How do I add the verification of my fields!
I'm trying to send emails like this [emailjs][1], but I realise that I can also submit my empty form! So I'm wondering how to adjust my code so that it takes into account the verification of fields!
I have followed the documentation on emailjs about submitting a form with a name, email, subject, and message but there is one last problem! I've been following the documentation on emailjs regarding the submission of a form with a name, email, subject, and message, but there is still a problem: I am able to send my message successfully and I receive the data on emailjs and in my gmail mailbox, but I can also submit the empty form! How can I fix this? thank you
import React, { Fragment, useRef } from 'react';
import emailjs from 'emailjs-com';
import { Link } from 'react-router-dom';
import { NotificationContainer, NotificationManager } from 'react-notifications';
function Contact() {
const form = useRef();
const sendEmail = (e) => {
e.preventDefault()
emailjs
.sendForm(
'service_yyyyy',
'template_zzzz',
form.current,
'user_ttttt'
)
.then(
(result) => {
NotificationManager.success('Thank you for trusting us, we come back to you sooner.', 'Successful!', 2000);
}, (error) => {
NotificationManager.error('erreur dans le formulaire!', 'erreurs');
});
e.target.reset()
}
return (
<Fragment>
<div className="regular-page-area section-padding-100">
<div className="container">
<div className="row">
<div className="col-12">
<div className="page-content" style= {{backgroundColor:'white'}}>
<h4>text</h4>
<p>text</p>
</div>
</div>
</div>
</div>
</div>
<section className="medium-padding120 bg-body contact-form-animation scrollme">
<div className="container">
<div className="row">
<div className="col col-xl-10 col-lg-10 col-md-12 col-sm-12 m-auto">
<div className="contact-form-wrap">
<div className="contact-form-thumb">
<div className="col-12">
<div className="section-heading">
<h3 style= {{color:'white'}}>Contact form</h3>
</div>
</div>
<h2 className="title"><span>SEND</span> <span>US YOUR</span><span>MESSAGE</span></h2>
<p>text</p>
<img src={require('../../images/crew.png')} alt="crew" className="crew" />
</div>
<form ref={form} onSubmit={sendEmail} className="contact-form">
<div className="col col-12 col-xl-12 col-lg-12 col-md-12 col-sm-12">
<div className="form-group label-floating">
<label className="control-label"> Name</label>
<input
type="text"
className='form-control'
placeholder='Name'
name='name'
/>
</div>
</div>
<div className="col col-12 col-xl-12 col-lg-12 col-md-12 col-sm-12">
<div className="form-group label-floating">
<label className="control-label"> Email</label>
<input
type="email"
className="form-control"
placeholder="youremail#gmail.com"
name='email'
/>
</div>
<div className="form-group label-floating">
<label className="control-label"> Subject</label>
<input
type='text'
className="form-control"
placeholder="Subject"
name='subject'
/>
</div>
<div className="form-group">
<textarea
name="message"
className="form-control"
placeholder="Your Message"
></textarea>
</div>
<button type="submit" className="btn btn-purple btn-lg full-width">Send Message</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div className="half-height-bg bg-white"></div>
</section>
<NotificationContainer/>
<Footer />
</Fragment>
)
}
export default Contact;

Reactjs - how can i take input from one component(inputbox) and use it in other component

I am building a MOVIE Search API app and I am taking input from one serah input box from the user with help of following code
import React, { Component } from "react";
import styles from "../styles/Example.module.css";
import Appletv from "../appletv.json";
import Link from 'next/link';
export default class example extends Component {
constructor(props) {
super(props);
this.state = {
movies: "",
filteredShows: Appletv.shows, // Starting with all shows
};
}
handMoviesChange = (event) => {
this.setState({
movies: event.target.value,
});
};
handleSubmit = (event) => {
event.preventDefault();
const newFilteredShows = Appletv.shows.filter((show) => {
return (
this.state.movies === "" ||
show.Title.toLowerCase().includes(this.state.movies.toLowerCase())
);
});
this.setState({ filteredShows: newFilteredShows });
};
render() {
console.table(Appletv.shows);
return (
<div className={styles.container}>
<h3 className={styles.Title}>SearchTest</h3>
<form onSubmit={this.handleSubmit}>
<div className={styles.row}>
<input
className={styles.input}
id="search"
type="input"
required
placeholder="Please enter title"
value={this.state.movies}
onChange={this.handMoviesChange}
/>
</div>
<Link href="/searchtest">
<button className={styles.button} type="submit">
Search
</button>
</Link>
</form>
<div className={styles.results} >
{this.state.filteredShows.map((filteredShows, index) => {
return (
<div>
{/* <h1 className={styles.title}>{filteredShows.Title}</h1>
<img
className={styles.image}
src={filteredShows.Thumbnail}
alt="Thumbnail"
/>
<h5 className={styles.genre}>
{filteredShows.Genre} & {filteredShows["Release date"]}
</h5>
<p className={styles.desc}>{filteredShows.Description}</p> */}
</div>
);
})}
</div>
</div>
);
}
}
Output of this code is following
and I want to get those results in the second component and second component code is following
import React, { Component, useState } from "react";
import styles from "../styles/Home.module.css";
import Appletv from "../appletv.json";
import Pagination from "./components/Pagination";
/* import Netflixlogo from "../logos/netflix.png";
*//* import Appletvlogo from "../logos/appletv.png";
import dstvlogo from "../logos/dstv.png";
import Amzonlogo from "../logos/amzon.png";
import Showmaxlogo from "../logos/showmax.png"; */
export default class searchtest extends Component {
constructor(props) {
super(props);
this.state = {
movies: "",
filteredShows: Appletv.shows, // Starting with all shows
};
}
handMoviesChange = (event) => {
this.setState({
movies: event.target.value,
});
};
handleSubmit = (event) => {
event.preventDefault();
/* const filteredShows = array.slice(0, 6);
*/
const newFilteredShows = Appletv.shows.filter((show) => {
return (
this.state.movies === "" ||
show.Title.toLowerCase().includes(this.state.movies.toLowerCase())
);
});
this.setState({ filteredShows: newFilteredShows });
};
render() {
console.table(Appletv.shows);
return (
<div className={styles.container}>
{/* <h3 className={styles.Title}>SearchTest</h3>
*/}
{/* <h4 className={styles.searche}>Search Results</h4>
*/}{" "}
<input
className={styles.searchin}
id="search"
type="input"
required
placeholder="Please enter title"
value={this.state.movies}
onChange={this.handMoviesChange}
/>{" "}
<button className={styles.searchbtn} type="submit">
Search
</button>{" "}
<div className={styles.row}>
<form onSubmit={this.handleSubmit}>
<div className={styles.row}>
<h4>Filtering Options</h4>
<label className={styles.label}>Movies</label>
<input
id="name"
className={styles.checkbox}
type="checkbox"
required
/>
</div>
<div className={styles.row}>
<label className={styles.label}>TV Show</label>
<input id="name" className={styles.checkbox} type="checkbox" />
</div>
<hr className={styles.hr} />
{/* <div className={styles.row}>
<input
className={styles.input}
id="search"
type="input"
required
placeholder="Please enter title"
value={this.state.movies}
onChange={this.handMoviesChange}
/>
</div>
<div className={styles.row}>
<input
className={styles.input}
type="text"
list="genre"
placeholder="Please Choose Genre"
/>
<datalist id="genre">
<option className={styles.option}>scifi</option>
<option>Documentry</option>
</datalist>
</div>
<div className={styles.row}>
<input
className={styles.input}
id="name"
type="input"
placeholder="Season Input"
/>
</div> */}
<div className={styles.row}>
<label className={styles.label}>Netflix</label>
<input className={styles.checkbox} id="name" type="checkbox" />
</div>
<div className={styles.row}>
<label className={styles.label}>Amazon Prime</label>
<input className={styles.checkbox} id="name" type="checkbox" />
</div>
<div className={styles.row}>
<label className={styles.label}>Apple TV+</label>
<input
className={styles.checkbox}
id="name"
type="checkbox"
required
/>
</div>
<div className={styles.row}>
<label className={styles.label}>Showmax</label>
<input
className={styles.checkbox}
id="name"
type="checkbox"
required
/>
</div>
<div className={styles.row}>
<label className={styles.label}>DSTV</label>
<input
className={styles.checkbox}
id="name"
type="checkbox"
required
/>
</div>
<hr className={styles.hr} />
<label for="cars" className={styles.label}>
Select Year
</label>
<div className={styles.row}>
<select
name="cars"
id="cars"
data-toggle="dropdown"
className={styles.dropdown}
>
{/* <option value=""></option>
*/} <option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
</select>
</div>
<label for="cars" className={styles.label}>
Select Genre
</label>
<div className={styles.row}>
<select
name="cars"
id="cars"
data-toggle="dropdown"
className={styles.dropdown}
>
{/* <option value=""></option>
*/} <option value="SCIFI">SCIFI</option>
<option value="comedy">COMEDY</option>
<option value="ADVENTURE">ADVENTURE</option>
<option value="DOCUMENTRY">DOCUMENTRY</option>
</select>
</div>
<label for="cars" className={styles.label}>
Select Age Restriction
</label>
<div className={styles.row}>
<select
name="cars"
id="cars"
data-toggle="dropdown"
className={styles.dropdown}
>
{/* <option value=""></option>
*/} <option value="SCIFI">18+</option>
<option value="comedy">Adult</option>
<option value="ADVENTURE">Kids</option>
<option value="DOCUMENTRY">Cartoons</option>
</select>
</div>
<button className={styles.button} type="submit">
Apply Filters
</button>
</form>
</div>
<div className={styles.results}>
{/* here we are going to do conditional rendering like if netflix checkbox clicked then show or else not */}
{/* <img className={styles.condimg}
src={""}
alt="condren"
/> */}
<h4 className={styles.h2}>Search Results for {this.state.title}</h4>
{this.state.filteredShows.slice(0,4).map((filteredShows, index) => {
return (
<div>
<h1 className={styles.title}>{filteredShows.Title}</h1>
<img
className={styles.image}
src={filteredShows.Thumbnail}
alt="Thumbnail"
/>
<h5 className={styles.genre}>
{filteredShows.Genre} & {filteredShows["Release date"]}
</h5>
<p className={styles.desc}>{filteredShows.Description}</p>
</div>
);
})}
</div>
<Pagination/>
</div>
);
}
}
and output of the second component is like this
and can anyone help me for getting data from the first component to another second component I know we can do it with help of props and I am not getting how can I do that can anyone help me with this.
you can pass state through React router
<Link
to={{
pathname: '/pathname',
state: { results: 'this is results' }
}}
/>
In the component you can access the variable like this:
componentDidMount: function() {
var results= this.props.location.state.results
},
hope it help :D
You can add a prop (function) onUpdate to your search component, then call this function when you submit your search. On the parent-component, you can then add logic to pass it to your other component. For example, you add it to a state, which is then passed to your second component.
handMoviesChange = (event) => {
this.setState({
movies: event.target.value,
});
this.props.onUpdate(event.target.value);
};
Then in your parent component:
<example onUpdate={(movies) => setState({ movies })} />
<searchtest movies={this.state.movies} />
Hope it helps.

React : How can I display the list of colleges in a dropdown list by hitting an api?

I am using Redux Forms and I am stuck at a point where I want to hit the API and get the list of colleges and display them in a dropdown list. How can I do it using Redux forms? I've used component Did Mount lifecycle method and by hitting the API, I am getting the response but I am not getting how to display it in this form in a dropdown manner?
Here's the code :
UserForm.js
import React, { Component } from 'react'
import { Field, reduxForm } from 'redux-form'
import {connect} from 'react-redux'
import axios from 'axios'
class UserForm extends Component {
componentDidMount(){
this.renderCollegeList('Middle')
}
renderCollegeList=async (name)=>{
const response = await axios.get(`http://universities.hipolabs.com/search`, {
params: { name }
})
console.log(response)
{this.props.college(response.data)}
}
renderInput(formProps) {
return (
<div>
<label>{formProps.label}</label>
<input {...formProps.input} type={formProps.type} max={formProps.max} autoComplete='off'
label={formProps.label} id={formProps.id}
checked={formProps.input.value} />
{formProps.meta.touched &&
(formProps.meta.error && <span>{formProps.meta.error}</span>)}
</div>
)
}
onSubmit = (formValues) => {
console.log('formValues', formValues)
this.props.onSubmit(formValues)
}
render() {
const { handleSubmit } = this.props
const current = new Date().toISOString().split("T")[0]
return (
<div className='container'>
<div className='row'>
<form onSubmit={handleSubmit(this.onSubmit)}
className='form'>
<div className='row mb-3'>
<label className='col-sm-2 col-form-label'>FullName</label>
<div className='col-sm-10'>
<Field name='fullname' component={this.renderInput}
type='text' className='form-control' />
</div>
</div>
<div className='row mb-3'>
<label className='col-sm-2 col-form-label'>Address</label>
<div className='col-sm-10'>
<Field name='address' component={this.renderInput}
type='text' />
</div>
</div>
<div className='row mb-3'>
<label className='col-sm-2 col-form-label'>BirthDate</label>
<div className='col-sm-10'>
<Field name='birthdate' component={this.renderInput}
type='date'
max={current} />
</div>
</div>
<div className='row mb-3'>
<label className='col-sm-2 col-form-label'>Select Your Gender</label>
<div className='col-sm-10'>
<div className='form-check'>
<label className='form-check-label'>Male</label>
<Field name='gender' component='input' type='radio' value='male'
/>{' '}
</div>
<div className='form-check'>
<label className='form-check-label'>Female</label>
<Field name='gender' component='input' type='radio' value='female'
/>{' '}
</div>
<div className='form-check'>
<label className='form-check-label'>Other</label>
<Field name='gender' component='input' type='radio' value='other'
/>{' '}
</div>
</div>
</div>
<div className='row mb-3'>
<label className='form-check-label'>Select Your Hobbies</label>
<div className='col-sm-10'>
<div className='form-check'>
<Field name='travelling' component={this.renderInput} type='checkbox' value='travelling'
label='Travelling' />
</div>
<div className='form-check'>
<Field name='reading' component={this.renderInput} type='checkbox' value='reading'
label='Reading' />
</div>
<div className='form-check'>
<Field name='gaming' component={this.renderInput} type='checkbox' value='gaming'
label='Gaming' />
</div>
<div className='form-check'>
<Field name='doodling' component={this.renderInput} type='checkbox' value='doodling'
label='Doodling' />
</div>
</div>
</div>
<div className='row'>
<div className='col-4 col-sm-4'>
<label>Select College</label>
<div className='col-8 col-sm-8'>
<Field name='college' component='select'>
<option value="">Select a college</option>
{this.props.college.map(collegeOption => (
<option value={collegeOption} key={collegeOption}>
{collegeOption}
</option>
))}
</Field>
</div>
</div>
</div>
<button type='submit'>Submit</button>
</form>
</div>
</div >
)
}
}
const validate = (formValues) => {
const errors = {}
if (!formValues.fullname) {
errors.fullname = 'You must enter a fullname'
}
if (!formValues.address) {
errors.address = 'You must enter the address'
}
if (!formValues.birthdate) {
errors.birthdate = 'Please select your date of birth'
}
if (!formValues.gender) {
errors.gender = 'Please select your gender'
}
return errors
}
const mapStateToProps=(state)=>{
return {college:state.users}
}
const formWrapped=reduxForm({
form: 'userform',
validate: validate
})(UserForm)
export default connect(mapStateToProps)(formWrapped)
You are trying to update props by using {this.props.college(response.data)}. This anti react pattern, Instead, maybe you need to add it to State and render component from State. e.g see below.
<Field name='college' component='select'>
<option value="">Select a college</option>
{this.state.college.map(collegeOption => (
<option value={collegeOption} key={collegeOption}>
{collegeOption}
</option>
))}
</Field>

How can I export a variable to another file React

I am doing a program which gets information from the https://api.randomuser.me/ API. I got the fetch call working but my goal is to display the information as prefilled information in a form located in another file, but I don't know how I should export the variables and how to use them in the other file.
This is my FetchCall.js:
import Axios from 'axios'
import { useState } from 'react';
function FetchCall() {
const[name,setName] = useState("")
const[birth,setBirth] = useState("")
const[email,setEmail] = useState("")
const[insuranceNo, setInsuranceNo] = useState("")
const[phoneNo, setPhoneNo] = useState("")
const getInfo = () => {
Axios.get("https://api.randomuser.me/").then(
(response) => {
setName(response.data.results[0].name.first + " " + response.data.results[0].name.last);
setBirth(response.data.results[0].dob.date);
setEmail(response.data.results[0].email)
setInsuranceNo(response.data.results[0].login.sha1)
setPhoneNo(response.data.results[0].phone)
}
);
};
return(
<div>
Hello
<button onClick={getInfo}>Get User</button>
<div>
{name}
</div>
<div>
{birth}
</div>
<div>
{email}
</div>
<div>
{insuranceNo}
</div>
<div>
{phoneNo}
</div>
</div>
);
}
export default FetchCall;
And here is where I want to export the variables (name, birth,email, insuranceNo,phoneNo)
import FetchCall from './fetchCall';
function InputTextFilled() {
return (
<div className="inputText has-text-left">
<fieldset disabled>
<div className="field">
<div className="control">
<p className="label">Full Name</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<a className="label">Date of Birth</a>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">National Insurance Number</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">Email Address</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">Telephone Number</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
</div>
</fieldset>
</div>
);
}
export default InputTextFilled;
React Props are like function arguments in JavaScript and attributes in HTML.
To send props into a component, use the same syntax as HTML attributes.
import Axios from "axios";
import { useState } from "react";
function FetchCall() {
const [name, setName] = useState("");
const [birth, setBirth] = useState("");
const [email, setEmail] = useState("");
const [insuranceNo, setInsuranceNo] = useState("");
const [phoneNo, setPhoneNo] = useState("");
const getInfo = () => {
Axios.get("https://api.randomuser.me/").then((response) => {
setName(
response.data.results[0].name.first +
" " +
response.data.results[0].name.last
);
setBirth(response.data.results[0].dob.date);
setEmail(response.data.results[0].email);
setInsuranceNo(response.data.results[0].login.sha1);
setPhoneNo(response.data.results[0].phone);
});
};
return (
<div>
Hello
<button onClick={getInfo}>Get User</button>
<div>{name}</div>
<div>{birth}</div>
<div>{email}</div>
<div>{insuranceNo}</div>
<div>{phoneNo}</div>
{/* //This is the child compoennt */}
<DisplayExample1
name={name}
birth={birth}
email={email}
insuranceNo={insuranceNo}
phoneNo={phoneNo}
/>
<DisplayExample2
name={name}
birth={birth}
email={email}
insuranceNo={insuranceNo}
phoneNo={phoneNo}
/>
</div>
);
}
export default FetchCall;
In a functional stateless component, the props are received in the function signature as arguments:
//Child Component Example 1
const DisplayExample1 = ({ name, birth, insuranceNo, phoneNo }) => (
<div className="inputText has-text-left">
<fieldset disabled>
<div className="field">
<div className="control">
<p className="label">Full Name</p>
<input className="input" type="text" value={name} />
</div>
<div className="control mt-5">
<a className="label">Date of Birth</a>
<input className="input" type="text" value={birth} />
</div>
<div className="control mt-5">
<p className="label">National Insurance Number</p>
<input className="input" type="text" value={insuranceNo} />
</div>
<div className="control mt-5">
<p className="label">Email Address</p>
<input className="input" type="text" value={email} />
</div>
<div className="control mt-5">
<p className="label">Telephone Number</p>
<input className="input" type="text" value={phoneNo} />
</div>
</div>
</fieldset>
</div>
);
React's props can pass data in React by defining custom HTML attributes to which you assign your data with JSX syntax. So don't forget the curly braces.
//Child Component Example 2 (Class Component)
class DisplayExample2 extends Component {
render() {
return (
<div className="inputText has-text-left">
<fieldset disabled>
<div className="field">
<div className="control">
<p className="label">Full Name</p>
<input className="input" type="text" value={this.props.name} />
</div>
<div className="control mt-5">
<a className="label">Date of Birth</a>
<input className="input" type="text" value={this.props.birth} />
</div>
<div className="control mt-5">
<p className="label">National Insurance Number</p>
<input
className="input"
type="text"
value={this.props.insuranceNo}
/>
</div>
<div className="control mt-5">
<p className="label">Email Address</p>
<input className="input" type="text" value={this.props.email} />
</div>
<div className="control mt-5">
<p className="label">Telephone Number</p>
<input className="input" type="text" value={this.props.phoneNo} />
</div>
</div>
</fieldset>
</div>
);
}
}

Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. In React

I am trying to add a spinner but there is a problem while executing the program
import React,{Component} from 'react';
import axios from 'axios';
import LoginFailModal from './LoginFailModal';
class Login extends Component
{
constructor(props)
{
super(props);
this.state = {
username : "",
password : "",
error:false,
loading:false
}
}
inputSet =(e)=> {
//console.log(e.target.name);
this.setState({[e.target.name]:e.target.value})
}
loginCheck =(e)=>{
this.setState({['error']:false});
this.setState({['loading']:true});
e.preventDefault();
var data = {
emph : this.state.username,
password : this.state.password
};
axios.post("url",data).then(response=>{
if(response.data.status=='200')
{
var response_data = response.data.data;
var user_data = {'token':response_data.token,
'user_type':'staf'}
localStorage.setItem('user_data', user_data);
this.setState({['error']:true,['loading']:false});
}
else
{
this.setState({['error']:true,['loading']:false});
}
})
}
render()
{
let {loading} = this.state;
return(
<>
{this.state.error ? (<LoginFailModal/>):""}
<div class="row">
<div className="col-xl-7 col-lg-4 col-md-2">
<img className="bg_img" src="assets/images/bg.jpg" alt=""/>
</div>
<div className="col-xl-5 col-lg-4 col-md-2">
<div className="login">
<img className="login-logo" src="assets/images/login-logo.png" alt=""/>
<form className="input">
<input className="text-field" type="text" name="username" onChange={this.inputSet} placeholder="Username" id=""/>
<input className="text-field" type="password" name="password" onChange={this.inputSet} placeholder="Password" id=""/>
<p className="fp">Forgot Password?</p>
<div className="cal">
<div className="row">
<div col-xl-4 col-lg-2 col-md-2>
<p className="sum">40+40</p>
</div>
<div col-xl-5 col-lg-2 col-md-2>
<input className="get" type="text" name="" placeholder="" id=""/>
</div>
</div>
</div>
<div className="row">
<p className="pl-5 remember"><input type="checkbox" name="remember me" id=""/>
Remember me</p>
</div>
<div className="login_button">
<p style={{color: "red"}}>{this.state.error}</p>
<button className="button" onClick={this.loginCheck}>Login
{loading && <i className="fa fa-refresh fa-spin" style={{color:'white'}}></i>}</button>
</div>
</form>
</div>
</div>
</div>
</>
)
}
}
export default Login;

Resources