Pracuję nad projektem React, w którym mam trzy komponenty Home, Studentslist, Card.
W tym projekcie Studentslist jest komponentem Parent to Card, a komponent Card to Child
Składnik listy uczniów. Teraz próbuję przekazać funkcje handlSubmit i handleChange z
Studentlist komponent do komponentu Karta za pomocą rekwizytów. Ale to nie działa dobrze.
Proszę, pomóż mi komuś przekazać funkcje handleSubmit i handleChange z Parent do
Dziecko w reakcji.
To jest Studentslist.js
import React, { useState } from 'react';
import axios from 'axios';
import './Studentslist.css';
import Card from '../../Components/Card/Card';
function Studentslist(props) {
const [show, setShow] = useState(false);
const [data, sendData] = useState({})
const postData = async() => {
try {
const result = await axios.post('http://localhost:5000/api/signup', data)
sendData(result)
console.log(result)
} catch(error) {
console.log(error)
}
}
const handleChange = ({ target }) => {
const { name, value } = target
const newData = Object.assign({}, data, { [name]: value })
sendData(newData)
}
const handleSubmit = (e) => {
e.preventDefault()
console.log(data)
postData()
}
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className='Departments'>
<button className='btn btn-primary'>Hockey</button>
<button onClick={() => setShow(true)} className='btn btn-primary ml-2'>Cricket</button>
</div>
<div className='table-responsive'>
<table className='table align-items-center table-flush'>
<thead className='thead-light'>
<tr>
<th scope='col'>No:</th>
<th scope='col'>Firstname</th>
<th scope='col'>Lastname</th>
<th scope='col'>Email</th>
<th scope='col'>Password</th>
<th scope='col'>Qualification</th>
<th scope='col'>Branch</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
{show && <Card setShow={() => setShow(false)}
firstname={'Firstname'}
lastname={'Lastname'}
email={'Email'}
password={'Password'}
qualification={'Qualification'}
branch={'Branch'}
firstNameValue={'firstname'}
lastNameValue={'lastname'}
emailValue={'email'}
passwordValue={'password'}
qualificationValue={'qualification'}
branchValue={'branch'}
handleChange={'handleChange()'}
handleSubmit={'handleSubmit()'}
>
</Card>}
</div>
</div>
</div>
)
}
export default Studentslist
To jest Card.js
import React, { useState } from 'react';
import './Card.css';
function Card(props) {
const { setShow } = props;
return (
<div className='container'>
<div className='row justify-content-center'>
<div className='col-6'>
<div className='Registration'>
<form onSubmit={props.handleSubmit}>
<div className="form-group">
<label htmlFor="firstname">{props.firstname}</label>
<input type="text" name={props.firstNameValue} onChange={props.handleChange} className="form-control" id="firstname"></input>
</div>
<div className="form-group">
<label htmlFor="lastname">{props.lastname}</label>
<input type="text" name={props.lastNameValue} onChange={props.handleChange} className="form-control" id="lastname"></input>
</div>
<div className="form-group">
<label htmlFor="email">{props.email}</label>
<input type="email" name={props.emailValue} onChange={props.handleChange} className="form-control" id="email"></input>
</div>
<div className="form-group">
<label htmlFor="password">{props.password}</label>
<input type="password" name={props.passwordValue} onChange={props.handleChange} className="form-control" id="password"></input>
</div>
<div className="form-group">
<label htmlFor="qualification">{props.qualification}</label>
<input type="text" name={props.qualificationValue} onChange={props.handleChange} className="form-control" id="qualification"></input>
</div>
<div className="form-group">
<label htmlFor="branch">{props.branch}</label>
<input type="text" name={props.branchValue} onChange={props.handleChange} className="form-control" id="branch"></input>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
<button type="button" onClick={setShow} className='cancel btn btn-danger ml-2'>Cancel</button>
</form>
</div>
</div>
</div>
</div>
)
}
export default Card
Jeśli uważasz, że moje wątpliwości nie są dla mnie jasne, dodaj komentarz Dziękuję.
2 odpowiedzi
Nie przekazujesz rekwizytów do Card
, to nie jest handleChange={'handleChange()'}
handleSubmit={'handleSubmit()'}
, zmień to
handleChange={handleChange}
handleSubmit={handleSubmit}
Przekazujesz ciąg do komponentu Card
, a nie odwołanie do funkcji. Wypróbuj poniższy kod.
<Card
...Other props
handleChange={this.handleChange}
handleSubmit={this.handleSubmit}
>
</Card>
Podobne pytania
Nowe pytania
reactjs
React to biblioteka JavaScript do tworzenia interfejsów użytkownika. Wykorzystuje deklaratywny paradygmat oparty na komponentach i ma być zarówno wydajny, jak i elastyczny.