Hi I am working on a React project in my project one button is coming soo many times. So I created one component for Button and I am reusing that component where ever I get a button. Now the problem is when I am trying to apply onClick function to that Button component, my function is not working so please help me to resolve this issue.
This is Button Component Button.js
import React from "react";
import "./Button.css";
const Button = () => {
return (
<div>
<button className="btn btn-primary">Show login button</button>
</div>
);
};
export default Button;
This is Button.css, I have written nothing in this
This is App.js
import React, { useState } from "react";
import Button from "./Button/Button";
import "./App.css";
const App = () => {
const [hideForm, setHideForm] = useState(false);
const showLoginForm = () => {
setHideForm(true);
};
return (
<div className="container">
<div className="row justify-content-center">
<div className="col-4">
{hideForm ? (
<div className="loginform">
<form>
<div className="form-group">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
id="email"
placeholder="Enter email"
></input>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
className="form-control"
id="password"
placeholder="Enter password"
></input>
</div>
<button type="submit" className="btn btn-primary mt-3">
Submit
</button>
</form>
</div>
) : (
<div className="signupform">
<form>
<div className="form-group">
<label htmlFor="firstname">Firstname</label>
<input
type="text"
className="form-control"
id="firstname"
placeholder="Enter firstname"
></input>
</div>
<div className="form-group">
<label htmlFor="lastname">Lastname</label>
<input
type="text"
className="form-control"
id="lastname"
placeholder="Enter lastname"
></input>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
id="email"
placeholder="Enter email"
></input>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
className="form-control"
id="password"
placeholder="Enter password"
></input>
</div>
<button type="submit" className="btn btn-primary mt-3">
Submit
</button>
</form>
</div>
)}
<div className="buttons mt-3">
<Button onClick={() => showLoginForm()}></Button>
</div>
</div>
</div>
</div>
);
};
export default App;
const Button = ({handleClick}) => {
return (
<div>
<button onClick={handleClick} className='btn btn-primary'>Show login button</button>
</div>
)
}
Use It like this:
<Button handleClick={()=>anyFunc()}/>
or
<Button handleclick={anyFunc}/>
``
You can pass the Parent's on click event to child as a props and when you click on the child component's button event that is being declared on the parent's component can be called. Here is how you can do it:
Code on the Parent Component:
const myClick =()=>{
// on click will be called here when button will be pressed
}
<Button onClick={this.myClick}></Button>
Code on the Child Component:
<button onClick={this.props.myClick}>Click Here</button>
Related
I'm working on a project which required ant design datepicker along with bootstrap.I'm not sure whats making this datepicker move out of screen. Is there something I need to do with bootstrap classes to fix this? I have shared my screenshot.thanenter image description here]1]1
const form= () => (
<form onSubmit={onFormSubmit}>
<div className="form-group">
<label className="btn btn-outline-secondary btn-block m-3">Add image
<input type="file" name="image" onChange={handleImageChange} accept="image/*" hidden/>
</label>
<input className="form-control m-2" type="text" name="title" placeholder="title" onChange={handleChange} value={title}/>
<textarea className="form-control m-2" type="text" name="content" placeholder="content" onChange={handleChange} value={content}/>
<AlgoliaPlaces className="form-control ml-2" placeholder="location" defaultValue={location} options={config} onChange={handleSearch} style={{height:"50px"}} />
<input className="form-control m-2" type="number" name="price" placeholder="price" onChange={handleChange} value={price}/>
<input className="form-control m-2" type="number" name="bed" placeholder="Number of beds" onChange={handleChange} value={bed}/>
</div>
**<DatePicker className="form-control m-2"/>**
<button className="btn btn-outline-primary m-2">Add</button>
</form>
)
return(
<div>
<div className="container-fluid p-5 text-center">
<h2>Add </h2>
</div>
<div className="container-fluid">
<div className="row">
<div className="col-md-10">
{hotelForm()}
</div
<div className="col-md-2">
<img src={preview} alt="preview-image" className="img img-fluid m-2" />
<pre>{JSON.stringify(values,null, 2)}</pre>
</div>
</div>
</div>
</div>
)
It looks like you are missing ant design css stylesheet.
Try to add the following line at the root of your app :
import 'antd/dist/antd.css';
I want to send an EmailJs with two parameters, the email that comes from the form, and the let toysList, that comes from the localStorage(and converted into a string via .toString()). How can I do that? I know how to connect the email because is in the form, but I don't know where i should put the toysList that is a variable.
import React from "react";
import emailjs from "emailjs-com";
let toysList = window.JSON.parse(localStorage.getItem("cart")).toString();
function ClientSubmit() {
function sendEmail(e) {
e.preventDefault();
emailjs
.sendForm(
"...",
"...",
e.target,
"..."
)
.then(
(result) => {
console.log(result.text);
},
(error) => {
console.log(error.text);
}
);
e.target.reset();
alert(
"Email send successfully!"
);
}
return (
<div>
<h3>Give us your mail and we will give you the price of the items</h3>
<form onSubmit={sendEmail}>
<div className="col-4">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
name="email"
placeholder="email#example.com"
/>
</div>
<div className="mb-3 col-2 ">
<input
type="submit"
className="btn btn-primary"
value="Send"
></input>
</div>
</form>
</div>
);
}
Insert a hidden in the form containing the value of the toyList so when you submit the form the toyList will be there.
<form onSubmit={sendEmail}>
<div className="col-4">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
name="email"
placeholder="email#example.com"
/>
<input
type="hidden"
className="form-control"
name="toyList"
defaultValue={toyList}
/>
</div>
<div className="mb-3 col-2 ">
<input
type="submit"
className="btn btn-primary"
value="Send"
></input>
</div>
</form>
I am working on a React project, In that I have one parent component that is Studentslist.js
in that component I have two buttons, one is Marketing button and another one is Computers button.
And I have one Card component that is Child for Studentslist.js.
Now first I have to hide Card component, I only want to show Card component when I click the Computers button.
I know how to do this by using Class based components but I am trying to do by using Function components.
This is Studentslist.js
import React, { useState } from 'react';
import './Studentslist.css';
import Card from '../../Components/Card/Card';
function Studentslist() {
const [hide, show] = useState({})
return(
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='Departments'>
<button className='btn btn-primary'>Marketing</button>
<button className='btn btn-primary ml-2'>Computers</button>
</div>
<Card></Card>
</div>
</div>
</div>
)
}
export default Studentslist
This is Card.js
import React from 'react';
import './Card.css';
function Card() {
return (
<div className='container'>
<div className='row justify-content-center'>
<div className='col-6'>
<div className='Registration'>
<form>
<div className="form-group">
<label htmlFor="firstname">Firstname</label>
<input type="text" className="form-control" id="firstname"></input>
</div>
<div className="form-group">
<label htmlFor="lastname">Lastname</label>
<input type="text" className="form-control" id="lastname"></input>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input type="email" className="form-control" id="email"></input>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input type="password" className="form-control" id="password"></input>
</div>
<div className="form-group">
<label htmlFor="qualification">Qualification</label>
<input type="text" className="form-control" id="qualification"></input>
</div>
<div className="form-group">
<label htmlFor="branch">Branch</label>
<input type="text" className="form-control" id="branch"></input>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
<button className='cancel btn btn-danger ml-2'>Cancel</button>
</form>
</div>
</div>
</div>
</div>
)
}
export default Card
If you feel I am not clear with my doubt, please put a comment.
This should work now.
import React, { useState } from 'react';
import './Studentslist.css';
import Card from '../../Components/Card/Card';
function Studentslist() {
const [show, setShow] = useState(false);
return(
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='Departments'>
<button className='btn btn-primary'>Marketing</button>
<button onClick={ () => setShow(!show)} className='btn btn-primary ml-2'>Computers</button>
</div>
{show && <Card></Card>}
</div>
</div>
</div>
)
}
export default Studentslist
function Studentslist() {
const [showCard, setShowCard] = useState(false);
return(
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='Departments'>
<button onClick = {() => setShowCard(true)} className='btn btn-primary'>Marketing</button>
<button className='btn btn-primary ml-2'>Computers</button>
</div>
{showCard && <Card></Card>}
</div>
</div>
</div>
)
}
export default Studentslist
This is how you can set up a toggle on the button.
const [toggle, setToggle] = useState(false)
<button className='btn btn-primary ml-2' onClick={() => setToggle(!toggle)>Computers</button>
{toggle && <Card></Card>}
import React , { Component } from 'react'
class Login extends Component{
constructor(props){
super(props)
}
render(){
return(
<form className="login-form">
<h1>login</h1>
<div>
<div className="form-group">
<label for="name">
Name :
</label>
<input name="name" type="text" value="" placeholder="Your Name" />
</div>
<div className="form-group">
<label for="password">
Password :
</label>
<input name="password" type="Password" value="" placeholder="Password" />
</div>
<input type="submit">Submit</input>
</div>
</form>
)
}
}
export default Login
I think the issue is here,
//input is an empty tag and you have provided Submit as children here
<input type="submit">Submit</input>
It should be simply this,
<input type="submit" value="Submit" />
ravibagul91's answer is correct. input is self-closing element. You cannot have children in it.
Alternatively, you may use button:
<button type="submit">Submit</button>
To this:
<input type="submit" value="Submit" />
I am creating a signup form that will render differently depending on whether the person signing up chooses Post a project or Work on a project from the very first div in the form.
How can I render each div that follows in the form-group conditionally based on what is selected in the first div? I am using hooks and I have found that most examples are for the extends Component approach.
My form:
const signUpForm = () => (
<form onSubmit={clickSubmit}>
<div className="form-group">
<select onChange={handleChange("role")} class="form-control">
<option selected>I want to...</option>
<option>Post a project</option>
<option>Work on a project</option>
</select>
</div>
<div className="form-group">
<input
onChange={handleChange("name")}
type="text"
placeholder="Name"
className="form-control"
value={name}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("email")}
type="email"
placeholder="Email"
className="form-control"
value={email}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("password")}
type="password"
placeholder="Password"
className="form-control"
value={password}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("studying")}
type="text"
placeholder="I'm studying..."
className="form-control"
value={studying}
/>
</div>
<div>
<div>{createInputs()}</div>
<button
className="btn btn-outline-primary btn-sm mb-3"
onClick={addSkill}
type="text"
>
Add more skills
</button>
</div>
<button onClick={clickSubmit} className="btn btn-primary" type="submit">
Sign Up
</button>
</form>
);
State:
const Signup = () => {
const [values, setValues] = useState({
name: "",
email: "",
password: "",
studying: "",
skills: [""],
error: "",
success: "",
role: ""
});
const { name, email, password, studying, skills, success, error, role } = values;
handleChange():
const handleChange = name => event => {
setValues({ ...values, error: false, [name]: event.target.value });
};
You should first divide the signUp in two parts and then call second function based on value of "role" from state.
The idea is to return the divs from second function based on state of first input.
const signUpForm = () => (
<form onSubmit={clickSubmit}>
<div className="form-group">
<select onChange={handleChange("role")} class="form-control">
<option selected>I want to...</option>
<option>Post a project</option>
<option>Work on a project</option>
</select>
</div>
{this.renderInput()}
<button onClick={clickSubmit} className="btn btn-primary" type="submit">
Sign Up
</button>
</form>
);
renderInput() {
if (values.role === "post") {
return (
<div className="form-group">
<input onChange={handleChange("name")} type="text" placeholder="Name" className="form-control" value={name} />
</div>
<div className="form-group">
<input onChange={handleChange("email")} type="email" placeholder="Email" className="form-control" value={email} />
</div>
<div className="form-group">
<input onChange={handleChange("password")} type="password" placeholder="Password" className="form-control" value={password} />
</div>
<div className="form-group">
<input onChange={handleChange("studying")} type="text" placeholder="I'm studying..." className="form-control" value={studying} />
</div>
<div>
<div>{createInputs()}</div>
<button className="btn btn-outline-primary btn-sm mb-3" onClick={addSkill} type="text">
Add more skills
</button>
</div>
);
}
}
You want to test the role state value is truthy/falsey and render the rest of your form on that value.
const SignUpForm = () => (
<form onSubmit={clickSubmit}>
<div className="form-group">
<select
defaultValue='unselected' // set default value here
onChange={handleChange("role")}
className="form-control" // fix className here, class alone isn't correct in react
>
<option value='unselected'>I want to...</option>
<option value='post'>Post a project</option>
<option value='work'>Work on a project</option>
</select>
</div>
{!!role && (
<Fragment>
<div className="form-group">
<input
onChange={handleChange("name")}
type="text"
placeholder="Name"
className="form-control"
value={name}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("email")}
type="email"
placeholder="Email"
className="form-control"
value={email}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("password")}
type="password"
placeholder="Password"
className="form-control"
value={password}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("studying")}
type="text"
placeholder="I'm studying..."
className="form-control"
value={studying}
/>
</div>
<div>
<div>{createInputs()}</div>
<button
className="btn btn-outline-primary btn-sm mb-3"
onClick={addSkill}
type="text"
>
Add more skills
</button>
</div>
</Fragment>
)}
<button onClick={clickSubmit} className="btn btn-primary" type="submit">
Sign Up
</button>
</form>
);