我是一个学习React的初学者。我试图跟进Udemy的一次演讲,但我的搜索框不能很好地工作,即使我重写了三次代码……你能检查一下为什么我的搜索框不能工作的代码吗?如果我在搜索框中输入一些单词,它应该显示匹配的卡片(在搜索框上写下姓名,它会显示匹配的卡片)
我将在下面分享我的代码
这是index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'tachyons';
ReactDOM.render(<App />, document.getElementById('root'));
reportWebVitals();这是App.js
import React, { Component } from 'react';
import CardList from './CardList';
import SearchBox from './SearchBox';
import {robots} from './robots';
import './App.css'
class App extends Component {
constructor() {
super()
this.state = {
robots: robots,
searchfield: ''
}
}
// With anything that comes from React, so constructor and render are pre-built in React
// any time you make your own methods on a componet, we have to use arror function
onSearchCange = (event) => {
this.setState({ searchfield: event.target.value })
// console.log(event.target.value);
// console.log(filteredRobots);
}
render() {
const filteredRobots = this.state.robots.filter(robots =>{
return robots.name.toLowerCase().includes(this.state.searchfield.toLowerCase());
})
return (
<div className='tc'>
<h1 className='f1'>RoboFriends</h1>
<SearchBox searchCange={this.onSearchCange} />
<CardList robots={filteredRobots}/>
</div>
);
}
}
export default App;这是SearchBox.jss
import React from 'react';
const SearchBox = ({ searchfield, searchChange}) => {
return (
<div className='pa2'>
<input
className='pa3 ba b--green bg-lightest-blue'
type='search'
placeholder='search robots'
onChange={searchChange}
/>
</div>
);
}
export default SearchBox;这是CardList.js
import React from 'react';
import Card from './Card';
const CardList = ({ robots }) => {
const cardComponent = robots.map((user, i) => {
return (<Card
key={i}
id={robots[i].id}
name={robots[i].name}
email={robots[i].email}
/>
);
})
return(
<div>
{cardComponent}
</div>
);
}这是Card.js
import React from 'react';
const Card = (props) => {
const {name, email, id} = props;
return (
<div className='tc bg-light-green dib br3 pa3 ma2 grow bw2 shawdow-5'>
<img alt='robots' src={`https://robohash.org/${id}?200*200`} />
<div>
<h2>{name}</h2>
<p>{email}</p>
</div>
</div>
);
}
export default Card;这是robots.js
export const robots = [
{
id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: 'Sincere@april.biz'
},
{
id: 2,
name: 'Ervin Howell',
username: 'Antonette',
email: 'Shanna@melissa.tv'
},
{
id: 3,
name: 'Clementine Bauch',
username: 'Samantha',
email: 'Nathan@yesenia.net'
},
{
id: 4,
name: 'Patricia Lebsack',
username: 'Karianne',
email: 'Julianne.OConner@kory.org'
},
{
id: 5,
name: 'Chelsey Dietrich',
username: 'Kamren',
email: 'Lucio_Hettinger@annie.ca'
},
{
id: 6,
name: 'Mrs. Dennis Schulist',
username: 'Leopoldo_Corkery',
email: 'Karley_Dach@jasper.info'
},
{
id: 7,
name: 'Kurtis Weissnat',
username: 'Elwyn.Skiles',
email: 'Telly.Hoeger@billy.biz'
},
{
id: 8,
name: 'Nicholas Runolfsdottir V',
username: 'Maxime_Nienow',
email: 'Sherwood@rosamond.me'
},
{
id: 9,
name: 'Glenna Reichert',
username: 'Delphine',
email: 'Chaim_McDermott@dana.io'
},
{
id: 10,
name: 'Clementina DuBuque',
username: 'Moriah.Stanton',
email: 'Rey.Padberg@karina.biz'
}
];谢谢你提前帮了我!
发布于 2021-10-18 17:11:34
您在App.js的第31行有一个拼写错误
<SearchBox searchCange={this.onSearchCange} />
你的事件是'searchChange‘而不是'searchCange’,修复后的代码行应该是这样的:
<SearchBox searchChange={this.onSearchCange} />
https://stackoverflow.com/questions/69619820
复制相似问题