import { StyleSheet, Text, View } from 'react-native'
import * as firebase from 'firebase';
//firebaseinititalized//
const[message,setMessage]=useState('');
const [messages, setMessages] = useState([])
useEffect(() => {
firebase.database.ref().child('messages').once('value',
snapshot=>{
const data=snapshot.val()
if(snapshot.val()){
const initMessages=[];
Object.
keys(data).
forEach(message=>initMessages.push(data[message]));
(initMessages)=>setMessages([initMessages]);
}
}
)
firebase.database.ref().child('messages').on("child_added",
snapshot=>{
const data=snapshot.val()
if(snapshot.val()){
(prevMessages)=> setMessages([data,...prevMessages]);
}
}
)
}, [])
const addItem =()=>{
if(!message) return;
const newMessage=firebase.database.ref().child().push();
newMessage.set(message,()=>setMessage(''));
}
function Hookfire() {
return (
<View >
<View >
<TextInput placeholder=" enter text message"
value={message} onChangeText={text=>setMessage(text)}/>
<Button title="send" onPress={addItem}/>
</View>
<FlatList data={messages}
renderItem={({item})=>
<View >
<Text >{item}</Text>
</View>}
/>
</View>
)
}
export default Hookfire
const styles = StyleSheet.create({})
error message shows that: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app