Error encountered: TypeError - The function formData.set is not supported. Unable to resolve this issue

Whenever I try to update the blog, an error occurs after reloading the browser. This error gets resolved when I refresh the browser before updating the blog. I have used formData.set in handleToggle and handleTagsToggle and it works fine there. However, I'm facing an issue with TypeError: formData.set is not a function at components/crud/BlogUpdate.js (186:17) @ Object.handleBody [as onChange]. How should I proceed to solve this problem?

TypeError: formData.set is not a function

Source
components/crud/BlogUpdate.js (186:17) @ Object.handleBody [as onChange]

  184 |     const handleBody = e => {
  185 |         setBody(e)
> 186 |         formData.set('body', e);
      |                 ^
  187 |     };
  188 | 
  189 |     const editBlog = (e) => {
Call Stack
Object.onEditorChangeText
node_modules/react-quill/lib/component.js (384:0)
Object.eval
node_modules/react-quill/lib/mixin.js (28:0)
...

import Link from 'next/link';
import { useState, useEffect } from 'react';
import Router from 'next/router';
import dynamic from 'next/dynamic';
import { withRouter } from 'next/router';
import { getCookie, isAuth } from '../../actions/auth';
import { getCategories } from '../../actions/category';
import { getTags } from '../../actions/tag';
import { singleBlog, updateBlog } from '../../actions/blog';
const ReactQuill = dynamic(() => import('react-quill'), { ssr: false });
import '../../node_modules/react-quill/dist/quill.snow.css';
import {QuillModules, QuillFormats} from '../../helpers/quill.js';

const BlogUpdate = ({router}) => {
    // Functionality for updating blogs goes here...
        

Answer №1

Problem

In the code snippet, the initial state of values.formData is an empty string, making the call to formData.set(...) invalid during the initial rendering.

const [values, setValues] = useState({
    title: '',
    error: '',
    success: '',
    formData: '', // <-- empty string
    title: '',
    body: ''
});

This issue arises after the initial render

useEffect(() => {
    setValues({
      ...values,
      formData: new FormData(), // <-- valid formData object after initial render
    });
    initBlog();
    initCategories();
    initTags();
}, [router])

Resolution

To fix this problem, ensure a valid initial state is provided

const [values, setValues] = useState({
    title: '',
    error: '',
    success: '',
    formData: new FormData(),
    title: '',
    body: ''
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using React hooks with Material-UI: Snackbar displaying only on first occasion and not again

I have identified an issue that can be easily reproduced. Steps to replicate: Step 1: Start a react app and include material-ui in the project: prompt> create-react-app mui-test prompt> cd mui-test prompt> yarn add @material-ui/core @material-ui ...

Tips for customizing the color of Menu in material-ui v5

I've been searching for solutions to change the background color of the Menu, but the methods I found are outdated. The use of @mui/styles and makeStyles is now deprecated, as stated in mui.com/styles/basics/#hook-api. I attempted to change the backgr ...

ES5 enables the extension of classes in React

This ES6 syntax works fine for me: import {Component} from 'react'; class A extends Component {} class B extends A { // I can redeclare some methods here } But how would one implement this using ES5? Like so: var React = require('reac ...

Displaying a div when a radio button is clicked in React

I am attempting to create a functionality where clicking on one radio button will display another div in React. As a beginner in React, I have tried implementing the code below but encountered issues with hiding and displaying the content based on user inp ...

What are the steps for creating a standalone build in nextJS?

Currently, I am undertaking a project in which nextJS was chosen as the client-side tool. However, I am interested in deploying the client as static code on another platform. Upon generating a build, a folder with all the proprietary server elements of ne ...

Compilation of Next.js with Supabase resulted in an error

While working on a project with Next.js and Supabase, everything was going smoothly until I added a table and generated TypeScript Definitions from the PostgreSQL schema using Supabase CLI. The problem arose when I tried running the "npm run build" command ...

What is the reason behind the delayed mutation of the state when invoking the setState method in React?

Currently diving into the Forms section within the documentation of ReactJS. Just implemented this code snippet to showcase the use of onChange (JSBIN). var React= require('react'); var ControlledForm= React.createClass({ getInitialState: f ...

After making the switch from TailwindCSS 2 to 3, certain classes like rotate and scale (which are among the familiar ones) have ceased to function

Yesterday, I decided to test out the new arbitrary value feature in TailwindCSS. It dawned on me that I needed to upgrade from version 2 to the latest version for it to work. Following the upgrade guide diligently, everything seemed fine until I noticed th ...

Having trouble with the production deployment of a Dockerized React application

As I work on containerizing my create-react-app for production using a Dockerfile and Docker-compose.yml, I am facing a challenge in creating the production build. The issue arises from the fact that the scripts required to run the build command are listed ...

Ways to customize individual thumbs when dealing with a Material UI slider that contains several thumbs

import React from 'react'; import PropTypes from 'prop-types'; import { withStyles, makeStyles } from '@material-ui/core/styles'; import Slider from '@material-ui/core/Slider'; import Typography from '@material- ...

Tailoring Hot Module Replacement using a webhook

Can HMR in React/NextJs be modified to listen for webhooks? I'm currently working with Contentful and find it tedious to reload the app every time I make changes. Since Contentful offers webhooks that can detect modifications, it would be great if my ...

Struggling with a React Native build warning problem?

Upon executing react-native init reactApp, I encountered a warning stating that npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ceef9fdffe8b1f2fde8f5eaf9dcacb2afa5b2ae">[email protected]</a> requires a pe ...

What is the process for inputting a predefined function into an interface?

In my project, I have a Locale interface that defines the properties of a locale for my component: interface Locale { src: string; alt: string; language: string; i18nFormat: string; } During debugging, I am using the built-in .toSource() function ...

Supabase authentication event listeners are triggering an endless cycle of re-rendering

My application is enclosed in a provider that is responsible for passing the session state throughout the entire app. However, I am encountering an issue where the Supabase auth listener code is causing my component to endlessly re-render and I am struggli ...

Issue: Unable to locate the 'stream/web' module while attempting to execute 'npm start' for a Next.js application, even though the Node version is correct

When I attempted to set up a new application using npm create next-app --typescript, everything seemed to be going smoothly. However, upon trying to start the project with npm start, an error message popped up stating "Error: Cannot find module 'strea ...

React - utilize a variable as the value for an HTML element and update it whenever the variable undergoes a change

I'm on a mission to accomplish the following tasks: 1.) Initialize a variable called X with some text content. 2.) Render an HTML paragraph element that displays the text from variable X. 3.) Include an HTML Input field for users to modify the content ...

What is the best approach for generating individual variables for every row in a React table?

Presently, I am developing a dynamic table using react and the structure is as follows: <TableBody> {finalLoadedTokensData.map((loadedToken, index) => { const isItemSelected = isSelected(loadedToken.contract); const label ...

What is the solution for correcting the error message "Prop `className` doesn't match. Server: 'MuiFormLabel-root-75....' "?

Currently using NextJS, I attempted to incorporate "@material-ui/core" into my project. Upon trying to utilize the TextField, I encountered the following error: index.js:2178 Warning: Prop className did not match. Server: "MuiFormLabel-root-75 MuiInpu ...

Removing the useEffect when the component is unmounted

I am encountering the following message: "Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a ...

How can you make the dropdown list in Autocomplete Material-UI open by default?

In a customized form, the information related to countries and their corresponding codes is visually presented. Rather than waiting for user focus, the drop-down list populates automatically with an array of preloaded data. Take a look at the sample code p ...