Home > Mobile >  react navigation - prop value being used as navigation setOption: paremeter key
react navigation - prop value being used as navigation setOption: paremeter key

Time:10-20

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'

CodePudding user response:

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 ]);
  • Related