87,907
社区成员
发帖
与我相关
我的任务
分享
import React from 'react';
import Axios from 'axios';
import Card from './Card';
import '../css/main.css'
import 'font-awesome/css/font-awesome.min.css';
class Main extends React.Component{
state = {
cards:[],
keyword:'',
}
componentDidMount(){
this.retreiveData();
}
retreiveData = () =>{
Axios.get(`${process.env.REACT_APP_API_URL}/cards`)
.then(response => {
this.state = {cards: response.data};
this.setState(this.state)
})
.catch(function(error){
console.log(error.response)
})
}
handleSearchChange =(e) =>{
console.log(this.state.keyword)
this.setState({keyword: e.target.value })
}
handleSearchSubmit = (e) => {
e.preventDefault();
this.retreiveData();
let newData =[];
var data = this.state.cards
data.map((item)=>{
if(item.companyName.indexOf(this.state.keyword) >= 0){
newData.push(item)
}
});
console.log(newData)
this.setState({cards:newData},()=>{
console.log(this.state.cards)
})
console.log(this.state.cards)
}
render(){
return (
<>
<section className="jumbotron text-center">
<div className="container">
<form className="form-group" onSubmit={this.handleSearchSubmit}>
<input type="text" className="form-control" placeholder="Search this site"
onChange={this.handleSearchChange}
/>
<button className="btn btn-secondary" type="submit">
<i className="fa fa-search"></i>
</button>
</form>
</div>
</section>
<Card cards={this.state.cards}/>
</>
);
}
}
export default Main;
this.setState({
count: this.state.count + 1
}, () => {
console.log('完成回调函数')