在(过滤的)搜索列表中仅使用一个useState挂钩

时间:2020-10-27 11:34:14

标签: reactjs filter use-state

packages/apps/securevpn/src/main/Android.mk: error: "securevpn (APPS android-arm64) missing libmaterial (JAVA_LIBRARIES android-arm64)" You can set ALLOW_MISSING_DEPENDENCIES=true in your environment if this is intentional, but that may defer real problems until later in the build. packages/apps/securevpn/src/main/Android.mk: error: "securevpn (APPS android-arm64) missing libcore (JAVA_LIBRARIES android-arm64)" You can set ALLOW_MISSING_DEPENDENCIES=true in your environment if this is intentional, but that may defer real problems until later in the build. packages/apps/securevpn/src/main/Android.mk: error: "securevpn (APPS android-arm64) missing libwork-runtime-ktx (JAVA_LIBRARIES android-arm64)" You can set ALLOW_MISSING_DEPENDENCIES=true in your environment if this is intentional, but that may defer real problems until later in the build. packages/apps/securevpn/src/main/Android.mk: error: "securevpn (APPS android-arm64) missing libmultidex (JAVA_LIBRARIES android-arm64)" You can set ALLOW_MISSING_DEPENDENCIES=true in your environment if this is intentional, but that may defer real problems until later in the build. packages/apps/securevpn/src/main/Android.mk: error: "securevpn (APPS android-arm64) missing android-arm64 (JAVA_LIBRARIES android-arm64)" You can set ALLOW_MISSING_DEPENDENCIES=true in your environment if this is intentional, but that may defer real problems until later in the build. build/make/core/main.mk:948: error: exiting from previous errors. 16:15:46 ckati failed with: exit status 1 #### failed to build some targets (01:21 (mm:ss)) #### 的当前设置here中,我使用多个ArticlePage.js钩子来渲染和过滤我的帖子列表:

useState()

我想知道,重构和优化此代码,我是否也只能(而不是上面两个)拥有?:

const [posts, setPosts] = useState([]);
const [filteredPosts, setFilteredPosts] = useState([]);

我只希望组件重新渲染:

  • 如果帖子数量发生变化
  • 如果在搜索输入更改中输入了值

我认为我的设置可以,但是我想知道我是否可以改善它?

1 个答案:

答案 0 :(得分:0)

我要替换

    let input = "PT9H"
    let formatter = DateFormatter()
    formatter.dateFormat = "'PT'HH'H'"

    let date = formatter.date(from: input)
    let time = "\(Calendar.current.component(.hour, from: date)).\(Calendar.current.component(.minute, from: date))"
    return "\(time)"

使用

const [filteredPosts, setFilteredPosts] = useState([]);

然后删除

const filteredPosts = useMemo(
    () => posts.filter((post) => post.title.toLowerCase().includes(search.toLowerCase())),
    [search, posts]
);