react navigation – prop value being used as navigation setOption: paremeter key

Is it possible to the something as follows:

const MyComponent = (position) => {

  useLayoutEffect(() => {
    navigation.setOptions({
      position: () => <CustomComponent />
    });
  }, [navigation, position]);

...
};

So the position value being passed to MyComponent will either be: ‘headerLeft’ or ‘headerRight’

Answer

Assuming that position is a string containing 'headerLeft' or 'headerRight', you could just create the object and pass it in:

useLayoutEffect(() => {
  let opts = { };
  opts[position] = () => <CustomComponent />;

  navigation.setOptions(opts);
}, [ navigation, position ]);