-1

In React.js I have a function which receives two parameters (id: string, User: UserDto), I want to use this function in two different components, on a first components, it works fine but on a second components, I need User to be not UserDto but UserDto[].

In my reusable function, how can I pass User so it can work as UserDto in one component and as UserDto[] in a second component?

3
  • 2
    Can you clarify your situation a bit more? In one component you have Your question leaves things a bit ambiguous, but I think you are saying that in one component you havefunction foo(id: string, User: UserDto) { .... } and in another component you want .... what? Commented Nov 27, 2023 at 16:08
  • in another component, function foo(id: string, User: UserDto[ ]){ .... } Commented Nov 27, 2023 at 16:20
  • But that would be an entirely different function, right? So you cannot "reuse" it. Commented Nov 27, 2023 at 16:22

1 Answer 1

1

You can modify component one to expect an array as well, and use the index 0 value passed to it. This would be fine, even if component one is only ever sent one value.

That way you can use the same function and pass the same parameters for both.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.