I get this error for the following code in React Native:
Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array object must have a [symbol.iterator]() method
counter-context.js
import React, {useState, createContext} from 'react'
export const CounterContext = createContext();
export const CounterContextProvider = props => {
const [count, setCount] = useState(0)
return (
<CounterContext.Provider value={[count, setCount]}>
{props.children}
</CounterContext.Provider>
);
}
counter-display.js
import React, { useContext } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { CounterContext, CounterContextProvider } from '../context/counter-context';
export default function Counter() {
const [count] = useContext(CounterContext);
return (
<CounterContextProvider>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>{count}</Text>
</View>
</CounterContextProvider>
)
}
const styles = StyleSheet.create({
sectionContainer: {
flex: 1,
padding: 24,
}
App.js
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={styles.body}>
<Counter/>
</View>
</ScrollView>
</SafeAreaView>
What is the reason? Seems to work in react.