CanВы можете просто изменить handleChange ()
и удалить эту строку ?? this.state.search = Test
из своего кода, потому что обновление состояния в render () не является правильным способом.
handleChange = (e) => {
const { name, value } = e.target;
this.setState({
[name]: value
})
var searchedResult= this.state.people.filter(item => item.name === this.state.name);
this.setState({
search: searchedResult
})
console.log('searched data =>',this.state.search);
}
inal Окончательный результат
import React, { Component } from 'react';
import { NativeSelect } from '@material-ui/core';
export default class Test extends Component {
constructor() {
super();
this.state = {
people: [
{
address: "Biratnagar",
bank: "Nabil Bank",
bankaccountnumber: 34343434,
baselocation: "Kathmandu",
basicpay: 5000,
branch: "telecom",
class: "class1",
confirmpassword: "nabanit123",
degree: "master",
designation: "engineer",
email: "nabanitkoirala@gmail.com",
id: "5e9fdebf0178920ed082241f",
mobilenumber: 445454545,
name: "Nabanit Koirala",
password: "nabanit123",
salary: 20000
},
{
address: "Biratnagar",
bank: "Nabil Bank",
bankaccountnumber: 34343434,
baselocation: "Kathmandu",
basicpay: 5000,
branch: "telecom",
class: "class1",
confirmpassword: "nabanit123",
degree: "master",
designation: "engineer",
email: "nabanitkoirala@gmail.com",
id: "5e9fdebf0178920ed082241f",
mobilenumber: 445454545,
name: "saroj kumar",
password: "nabanit123",
salary: 30000
},
{
address: "Biratnagar",
bank: "Nabil Bank",
bankaccountnumber: 34343434,
baselocation: "Kathmandu",
basicpay: 5000,
branch: "Account",
class: "class1",
confirmpassword: "nabanit123",
degree: "master",
designation: "engineer",
email: "nabanitkoirala@gmail.com",
id: "5e9fdebf0178920ed082241f",
mobilenumber: 445454545,
name: "kamlesh shresths",
password: "nabanit123",
salary: 5000
},
{
address: "Biratnagar",
bank: "Nabil Bank",
bankaccountnumber: 34343434,
baselocation: "Kathmandu",
basicpay: 5000,
branch: "IT",
class: "class1",
confirmpassword: "nabanit123",
degree: "master",
designation: "engineer",
email: "nabanitkoirala@gmail.com",
id: "5e9fdebf0178920ed082241f",
mobilenumber: 445454545,
name: "kumar thapa",
password: "nabanit123",
salary: 50000
},
],
name: '',
isLoading: true,
search: ''
}
}
handleChange = (e) => {
const { name, value } = e.target;
this.setState({
[name]: value
})
var searchedResult= this.state.people.filter(item => item.name === this.state.name);
this.setState({
search: searchedResult
})
console.log('searched data =>',this.state.search);
}
handleSubmit = (e) => {
e.preventDefault();
this.setState({
isLoading: false
})
console.log("handle submit>>", this.state);
}
componentDidMount() {
this.setState({
search: Test
})
}
render() {
console.log("component did mount>>", this.state);
const { search } = this.state;
let Test = this.state.isLoading
? <p>Isloading</p>
: this.state.people.filter(item => item.name === this.state.name).map(item => (
<div>
<p>key:{item.name}</p>
<div>
<p>add:{item.address}</p>
<p>bank:{item.bank}</p>
<p>bankaccount:{item.bankaccountnumber}</p>
<p>baselocation:{item.baselocation}</p>
<p>basicpay:{item.basicpay}</p>
<p>branch:{item.branch}</p>
<p>class:{item.class}</p>
<p>confirmpassword:{item.confirmpassword}</p>
<p>degree:{item.degree}</p>
<p>designation:{item.designation}</p>
<p>email:{item.email}</p>
<p>mobilenumber:{item.mobilenumber}</p>
<p>name:{item.name}</p>
<p>password:{item.password}</p>
<p>salary:{item.salary}</p>
</div>
</div>
))
// this.state.search = Test;// Do not do like this
return (
<div>
<NativeSelect defaultValue="" name="name" onChange={this.handleChange}>
<option value="choose">choose</option>
<option value="Nabanit Koirala">Nabanit Koirala</option>
<option value="Saroj Kumar">Saroj Kumar</option>
</NativeSelect>
<button type="submit" onClick={this.handleSubmit}>Search</button>
<div>
{Test}
</div>
</div>
)
}
}