简体   繁体   English

我正在尝试在反应中实现动态搜索

[英]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:您的代码中有几个问题:

  1. StudentContainer . StudentContainer You should never set state variable in constructor via props.你永远不应该通过 props 在构造函数中设置状态变量。 You should directly pass props as coming from children:你应该直接传递来自孩子的道具:

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>
    );
  }
}

  1. In StudentCard You again set state value via props .StudentCard您再次通过 props 设置状态值。 But it will only set first time when component is mounted.但它只会在安装组件时第一次设置。 It will not going to set again.它不会再次设置。 That is the reason your component is not updating.这就是您的组件未更新的原因。 Do this (add condition according to yours requriment. This ensure component re-render again)这样做(根据您的要求添加条件。这确保组件再次重​​新渲染)
 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM