1- import React , { useState , useEffect , useContext } from 'react'
1+ import React , { useState , useEffect , useContext , useCallback } from 'react'
22import { Link } from '@reach/router'
33import Button from './Button'
44
@@ -39,8 +39,6 @@ const Comments = (props) => {
3939
4040 const [ comment , setComment ] = useState ( '' )
4141
42- const [ fetchComments , setFetchComments ] = useState ( false )
43-
4442 const [ commentId , setCommentId ] = useState ( )
4543
4644 const [ commentReply , setCommentReply ] = useState ( '' )
@@ -55,13 +53,14 @@ const Comments = (props) => {
5553
5654 const [ replyAttachments , setReplyAttachments ] = useState ( [ ] )
5755
56+ const fetchStoryComments = useCallback ( async ( ) => {
57+ const response = await userStory . getComments ( storyId )
58+ setComments ( response . data . data . userStory . user_story_comments )
59+ } , [ storyId ] )
60+
5861 useEffect ( ( ) => {
59- const fetchStoryComments = async ( ) => {
60- const response = await userStory . getComments ( storyId )
61- setComments ( response . data . data . userStory . user_story_comments )
62- }
6362 fetchStoryComments ( )
64- } , [ storyId , fetchComments ] )
63+ } , [ fetchStoryComments ] )
6564
6665 useEffect (
6766 ( ) => ( ) => {
@@ -82,7 +81,8 @@ const Comments = (props) => {
8281 await userStory . postComment ( formData )
8382 setComment ( '' )
8483 setAttachments ( [ ] )
85- setFetchComments ( ! fetchComments )
84+
85+ fetchStoryComments ( )
8686 }
8787
8888 const addCommentReply = async ( data ) => {
@@ -97,8 +97,9 @@ const Comments = (props) => {
9797 await userStory . postCommentReply ( formData )
9898 setCommentReply ( '' )
9999 setReplyAttachments ( [ ] )
100- setFetchComments ( ! fetchComments )
101100 setRepliesToggled ( null )
101+
102+ fetchStoryComments ( )
102103 }
103104
104105 return (
0 commit comments