[英]i'm trying to implement dynamic search in react
Hi all I'm trying to implement dynamic search in react, but I'm not able to render the desired content.大家好,我正在尝试在 React 中实现动态搜索,但我无法呈现所需的内容。
please find the code at https://codesandbox.io/s/gracious-feather-1lqnm?file=/src/App.js请在https://codesandbox.io/s/gracious-feather-1lqnm?file=/src/App.js找到代码
when i try to filter out dynamic search results当我尝试过滤掉动态搜索结果时
initial screen please click on the link to view the image when I try to search for "bert" i got the correct results in logs please click on the link to view the image but I'm not able to render the same, looks like I'm able to render the required number of results, but not the desired ones.初始屏幕,请点击链接查看图像时,我尝试搜索“伯特”我得到了在日志中正确的结果请点击链接查看图像,但我不能够呈现相同的,看起来像我'能够呈现所需数量的结果,但不能呈现所需的结果。 please click on the link to view the image
请点击链接查看图片
There are couple of issues in your code:您的代码中有几个问题:
StudentContainer
. StudentContainer
。 You should never set state variable in constructor via props.
class StudentContainer extends Component {
render() {
return (
<div>
{this.props.students.length
? this.props.students.map(
(
{ firstName, lastName, email, company, skill, grades, pic },
idx
) => (
<ListItem xs={12} sm={6} md={3}>
<StudentCard
key={idx + 1}
firstName={firstName}
lastName={lastName}
email={email}
company={company}
skill={skill}
grades={grades}
pic={pic}
/>
</ListItem>
)
)
: "no data"}
</div>
);
}
}
StudentCard
You again set state value via props .StudentCard
您再次通过 props 设置状态值。 But it will only set first time when component is mounted. componentDidUpdate(prevProps, props) {
if (
prevProps.firstName !== props.firstName ||
prevProps.email !== props.email
) {
this.setState({
firstName: this.props.firstName,
lastName: this.props.lastName,
email: this.props.email,
company: this.props.company,
skill: this.props.skill,
grades: this.props.grades,
pic: this.props.pic
});
}
}
Here is the demo: https://codesandbox.io/s/xenodochial-hofstadter-0h0yd?file=/src/Components/StudentContainer.js这是演示: https : //codesandbox.io/s/xenodochial-hofstadter-0h0yd?file=/ src/Components/ StudentContainer.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.