使用StackNavigator时遇this.props.header不是函数错误求助
Hey there, let's break down and fix this error you're hitting. The root cause here is almost certainly tied to how you're using the @ercpereda/react-native-accordion component in your PassportScreen—this component expects the header prop to be a function that returns a React element, but you're likely passing a raw JSX element or non-function value instead.
Here's how to resolve it step by step:
1. Correct the Accordion Component Usage
First, double-check where you're rendering the Accordion component. The header prop must be a function, not direct JSX.
Wrong (causes the error):
<Accordion header={<Text>My Accordion Header</Text>} content={<View>...</View>} />
Correct:
<Accordion // header is a function that returns your desired header component header={() => <Text>My Accordion Header</Text>} // content should also be a function for consistency content={() => <View>...</View>} />
2. Adjust Your renderItem Function
Since you're using a FlatList with ListItem, you probably have the Accordion inside your renderItem method. Update it to pass a function to the header prop, like this:
renderItem = ({ item }) => { return ( <Accordion header={() => ( <ListItem title={item.title} subtitle={item.subtitle} chevron // keeps the expand/collapse indicator /> )} content={() => ( <View style={styles.accordionContent}> <Text>{item.details}</Text> {/* Add any other content you want to show when expanded */} </View> )} /> ); };
3. Additional Checks
- If you're passing dynamic data to the Accordion, make sure each item's header value is wrapped in a function. Avoid passing static JSX directly—always wrap it in
() => {}. - Verify there's no conflict with other
headerprops elsewhere in your code (like custom navigation headers). Your currentnavigationOptionsonly sets a title, so this shouldn't be an issue here.
Once you adjust the Accordion's header prop to be a function, the error should disappear, and your accordions will render correctly in the PassportScreen.
内容的提问来源于stack exchange,提问作者lmotl3

