reactjsreact-functional-component

How to use same function in two components where in 1st component, one parameter is an dto and in a second component, its dto array, in React


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?


Solution

  • 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.