有可能这样做吗?
const MyComponent = (position) => {
useLayoutEffect(() => {
navigation.setOptions({
position: () => <CustomComponent />
});
}, [navigation, position]);
...
};
所以传递给MyComponent的position
值要么是'headerLeft'要么是'headerRight'
假设position
是包含'headerLeft'
或'headerRight'
的字符串,您可以创建对象并将其传入:
useLayoutEffect(() => {
let opts = { };
opts[position] = () => <CustomComponent />;
navigation.setOptions(opts);
}, [ navigation, position ]);