0
class PostIndex extends Component {

    //define state
    const [posts, setPosts] = useState([]);

    //useEffect hook
    useEffect(() => {

        //panggil method "fetchData"
        fectData();

    }, []);

    //function "fetchData"
    const fectData = async () => {
        //fetching
        const response = await axios.get('http://localhost:3000/api/posts');
        //get response data
        const data = await response.data.data;

        //assign response data to state "posts"
        setPosts(data);
    
    }
}


export default PostIndex;

please help me, i new learning at react js. i was about make class name PostIndex, so i change from function PostIndex() to class PostIndex extends Component. and get error from this line const [posts, setPosts] = useState([]); could you help me? please. thanks

2
  • 1
    You can't use hooks in the class components, check docs rather use functional component: const PostIndex = () => { const [posts, setPosts] = useState([]); // rest of your code.... } export default PostIndex; Commented Jun 7, 2022 at 7:33
  • okay im sorry, i was learning, thanks for give me reference Commented Jun 7, 2022 at 7:36

3 Answers 3

1

You cant use react class components with react hooks.

So the right solution will be something like this:

const PostIndex = () => {

  //define state
  const [posts, setPosts] = useState([]);

  //useEffect hook
  useEffect(() => {

      //panggil method "fetchData"
      fectData();

  }, []);

  //function "fetchData"
  const fectData = async () => {
      //fetching
      const response = await axios.get('http://localhost:3000/api/posts');
      //get response data
      const data = await response.data.data;

      //assign response data to state "posts"
      setPosts(data);
  }

  return your markup
}
Sign up to request clarification or add additional context in comments.

2 Comments

so i can't use class if there is useState?
Yes, react hooks only works with functional components
1

You can't use hooks in the class components, check docs rather use functional component:

const PostIndex = () => {
  const [posts, setPosts] = useState([]);
  // rest of your code....
  }

  export default PostIndex;

1 Comment

thank for share new things for me, its so usefull for me
1

You can't use React Hooks such as useState in class component. It only works in functional component. If you want to use state in class component, please follow as:

class PostIndex extends React.Component {
   state = {posts: []}; // define state
        .....
   this.setState({
       posts: ['a', 'b']
   }); // set state
        .....
   let temp = this.state.posts; // use state
        .....

I hope this will help you. Thanks.

3 Comments

thanks for giving me sample code, i will try it
but can i call fectdata function without useEffect hook?
Of course, you can call fetchdata function without useEffect hook. You can use componentDidMount() lifecycle instead of useEffect in class component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.