Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How can I use useState here?

I'm trying to archive a course using the code below. It works, but the error "Uncaught (in promise) SyntaxError: Unexpected token T in JSON at position 0" appears and I have to refresh the page for the component to update. Now I know useState can be used here but I can't get it to work so I removed it prior to sending this question.

Any help is appreciated as I'm kinda new to all these react stuff.

import React, { } from 'react';
import { Button } from 'react-bootstrap';
import Swal from 'sweetalert2';

export default function ArchiveCourse({ course, isActive, fetchData }) {


    const archiveToggle = (courseId) => {

        fetch(`http://localhost:4000/courses/${courseId}/archive`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${localStorage.getItem('accessToken')}`
            }
        })
            .then(res => res.json())
            .then(data => {
                console.log(data)
                if (data === true) {
                    Swal.fire({
                        title: 'Success',
                        icon: 'success',
                        text: 'Course successfully disabled'
                    })
                    fetchData();
                } else {
                    Swal.fire({
                        title: 'Something Went Wrong',
                        icon: 'Error',
                        text: 'Please Try again'
                    })
                    fetchData();
                }


            })
    }

    const unArchiveToggle = (courseId) => {
        fetch(`http://localhost:4000/courses/${courseId}/reactivate`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${localStorage.getItem('accessToken')}`
            }
        })
            .then(res => res.json())
            .then(data => {
                console.log(data)
                if (data === true) {
                    Swal.fire({
                        title: 'Success',
                        icon: 'success',
                        text: 'Course successfully enabled'
                    })
                    fetchData();
                } else {
                    Swal.fire({
                        title: 'Something Went Wrong',
                        icon: 'Error',
                        text: 'Please Try again'
                    })
                    fetchData();
                }
            })
    }

    return (
        <>
            {isActive ?

                <Button variant="danger" size="sm" onClick={() => archiveToggle(course)}>Archive</Button>

                :

                <Button variant="success" size="sm" onClick={() => unArchiveToggle(course)}>Unarchive</Button>

            }
        </>

    )
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do something like this:`

import React, { useState } from 'react';
import { Button } from 'react-bootstrap';
import Swal from 'sweetalert2';

export default function ArchiveCourse({ course, isActive, fetchData }) {
    const [active, setActive] = useState(isActive);

    const archiveToggle = (courseId) => {

        fetch(`http://localhost:4000/courses/${courseId}/archive`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${localStorage.getItem('accessToken')}`
            }
        })
            .then(res => res.json())
            .then(data => {
                console.log(data)
                if (data === true) {
                    Swal.fire({
                        title: 'Success',
                        icon: 'success',
                        text: 'Course successfully disabled'
                    })
                    setActive(false); //If you want to active status
                    fetchData();
                } else {
                    Swal.fire({
                        title: 'Something Went Wrong',
                        icon: 'Error',
                        text: 'Please Try again'
                    })
                    fetchData();
                }


            })
    }

    const unArchiveToggle = (courseId) => {
        fetch(`http://localhost:4000/courses/${courseId}/reactivate`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                Authorization: `Bearer ${localStorage.getItem('accessToken')}`
            }
        })
            .then(res => res.json())
            .then(data => {
                console.log(data)
                if (data === true) {
                    Swal.fire({
                        title: 'Success',
                        icon: 'success',
                        text: 'Course successfully enabled'
                    })
                    setActive(true); //If you want to active status
                    fetchData();
                } else {
                    Swal.fire({
                        title: 'Something Went Wrong',
                        icon: 'Error',
                        text: 'Please Try again'
                    })
                    fetchData();
                }
            })
    }

    return (
        <>
            {isActive ?

                <Button variant="danger" size="sm" onClick={() => archiveToggle(course)}>Archive</Button>

                :

                <Button variant="success" size="sm" onClick={() => unArchiveToggle(course)}>Unarchive</Button>

            }
        </>

    )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!