What are examples of utility functions in React?
Formatting a date, sorting an array, calculating a total, and formatting a price. These are pure functions that take inputs and return outputs with no state or effects, so they do not need to be hooks.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Custom Hooks vs Utility Functions: How to Choose in React
If the logic uses state or effects, use a custom hook. If it is pure, taking inputs and returning outputs with no hooks, use a utility function. The presence of hooks is the deciding factor.
A utility function is a plain function with no state or side effects, usable anywhere. A custom hook starts with 'use', uses React hooks internally, and can only be called from components or other hooks.
Because it adds the rules of hooks for no benefit. Pure logic as a utility function is simpler, can be used outside React, and is easier to test. Wrapping it in a hook creates unnecessary constraints.
Still have questions?
Browse all our FAQs or reach out to our support team
