Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useArrayFind): new function #1875

Merged
merged 3 commits into from Jul 17, 2022
Merged

Conversation

yjl9903
Copy link
Contributor

@yjl9903 yjl9903 commented Jul 12, 2022

Description

Reactively array find.

import { useArrayFind } from '@vueuse/core'
const positive = useArrayFind([ref(1), ref(-1), ref(2)], val => val > 0)
// positive.value: 1
import { useArrayFind } from '@vueuse/core'
const list = reactive([-1, -2])
const positive = useArrayFind(list, val => val > 0)
// positive.value: undefined
list.push(1)
// positive.value: 1

Additional context

#1812 (comment)


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@yjl9903 yjl9903 mentioned this pull request Jul 12, 2022
13 tasks
fn: (element: T, index: number, array: MaybeComputedRef<T>[]) => boolean,
): ComputedRef<T | undefined> {
return computed(() => {
const find = Array.prototype.find.bind(resolveUnref(list))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to bind it?

Copy link
Contributor Author

@yjl9903 yjl9903 Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy this code from #1831 @LittleSound (

I also think there may be no need to bind.

@antfu antfu merged commit d00b7ed into vueuse:next Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants