anyone-oslo/pages

View on GitHub
app/javascript/components/RichTextArea/useMaybeControlledValue.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { useState } from "react";

export default function useMaybeControlledValue<T>(
  initialValue: T,
  onChange?: (nextValue: T) => void
): [T, (nextValue: T) => void] {
  const [value, setValue] = useState(initialValue);

  if (onChange) {
    return [initialValue, onChange];
  } else {
    return [value, setValue];
  }
}