Skip to main content
Version: Current (v7.0.0-beta.3)

A custom AnyOfField or OneOfField reads the selection through useOptionSelection

Detect​

  • In your code: a component passed as AnyOfField or OneOfField in fields= that holds the selected option index in useState, a class state or a ref.
  • On the rendered page: such a field shows one option while the extra keys of the object beside it render for another.

Change​

AnyOfField and OneOfField read and write the selection with useOptionSelection(), which @rjsf/core exports, and SchemaField reads it to stub an object's extra keys with the selected option. A custom replacement that keeps the selection in its own state shows one option while the object beside it is stubbed for another. Read and write it through the hook; the options prop stays:

import { useOptionSelection } from '@rjsf/core';

const { selectedOption, selectOption } = useOptionSelection(registry, fieldPath, options, formData, discriminator);
// on a pick: selectOption(index), then onChange(dataForTheNewOption, fieldPath)

The options it receives are already retrieved; see AnyOfField and OneOfField expect their options already retrieved.

Verify​

Also read: