functional react component did mount code example
Example 1: how to use componentdidmount in functional component
useEffect(() => {
if(!props.fetched) {
props.fetchRules();
}
console.log('mount it!');
}, []);
useEffect({
your code here
})
useEffect(() => {
}, [yourDependency]);
useEffect(() => {
return () => {
}
}, [yourDependency]);
Example 2: useeffect componentdidmount
useEffect(() => {
messagesRef.on('child added', snapshot => {
const message = snapshot.val();
message.key = snapshot.key;
setMessages(messages.concat(message));
}, []);