question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is a value on frontend but no value is sent via form

See original GitHub issue

So I had this working react-select before I changed my style.scss. After a few styling adjustments, it suddenly didn’t work anymore. Well at the frontend everything seems to work. Value is put in and even the props of the component are the same as my old code. But when I send the form data to my google spreadsheet, no value is sent. The rest of the data like email and name just works fine.

image Top is what it should look like and bottom the current issue

image image proof that the value is on the frontend (same output as in my old code)

This is my code:

<Select
    placeholder="Wähle deine Kategorien"
    name="tags"
    className="basic-multi-select"
    isMulti
    options={allTagsSelect}
    theme={(theme) => ({
    ...theme,
    borderRadius: 4,
    colors: {
      ...theme.colors,
      primary25: '#19F8E5',
      primary: '#19F8E5',
      neutral0: '#282828',
      neutral20: '#4e4e4e'
      }
    })}
  required
  delimiter=" | "/>

  // sending form code if this helps you (same as the old code but with `setLoading`, this is just used for a loading container):
  useEffect(() => {
    const scriptURL = 'https://script.google.com/macros/s/XXXXXXXXXXXXXXXXXXX' +
        'XXXXXXXXXXXXXXXXXXXXX/exec'
    const form = document.forms['submit-to-google-sheet']

    loadCaptchaEnginge(4, '#222', 'white', 'numbers');

    form.addEventListener('submit', e => {
      setLoading(true);
      e.preventDefault();
      if (validateCaptcha(refCaptcha.current.value) === true) {
        fetch(scriptURL, {
          method: 'POST',
          body: new FormData(form)
        }).then(() => {
          setLoading(false);
          setSent(true);
        }).catch(error => console.error('Error!', error.message));
      } else {
        setLoading(false);
        toast.error('Captcha war nicht richtig.', {
          position: "bottom-center",
          autoClose: 5000,
          hideProgressBar: true,
          closeOnClick: true,
          pauseOnHover: true,
          draggable: false,
          progress: undefined
        });
      }
    })
  }, [])

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
AdamHasmacommented, Nov 17, 2021

I keep forgetting that the entries iterator is different than an array. Here’s how to loop through the FormData.

    const body = new FormData(e.currentTarget);
    for (const entry of body.entries()) {
      console.log(`${entry[0]}: ${entry[1]}`);
    }

I updated the codesandbox here to reflect this.

Perhaps in the useEffect hook you want to lead with e.preventDefault before changing state or instead of adding an eventListener directly to the DOM item, try adding the onSubmit event directly to your animated.form.

The one clue I see is when the form is rejected by the captcha, the Select’s value is being cleared. I’m not entirely sure what’s causing this, but it does seem that something is clearing it via the validation or causing it to re-render.

In either case, it doesn’t seem to be related to a react-select issue.

It works! Thank you so much. I did what you suggested.

0reactions
ebonowcommented, Nov 11, 2021

I keep forgetting that the entries iterator is different than an array. Here’s how to loop through the FormData.

    const body = new FormData(e.currentTarget);
    for (const entry of body.entries()) {
      console.log(`${entry[0]}: ${entry[1]}`);
    }

I updated the codesandbox here to reflect this.

Perhaps in the useEffect hook you want to lead with e.preventDefault before changing state or instead of adding an eventListener directly to the DOM item, try adding the onSubmit event directly to your animated.form.

The one clue I see is when the form is rejected by the captcha, the Select’s value is being cleared. I’m not entirely sure what’s causing this, but it does seem that something is clearing it via the validation or causing it to re-render.

In either case, it doesn’t seem to be related to a react-select issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Input value doesn't display. How is that possible?
I have a small form with a handful of text inputs, 1 textarea and 2 selects. None of the text input values display,...
Read more >
Sending form data - Learn web development | MDN
The names and values of the non-file form controls are sent to the server as name=value pairs joined with ampersands. The action value...
Read more >
HTML input value Attribute - W3Schools
For "checkbox", "radio", "image" - it defines the value associated with the input (this is also the value that is sent on submit)....
Read more >
How to Convert HTML Form Field Values to a JSON Object
The FormData API doesn't directly convert form values to JSON, but we can get there by using the entries method and passing its...
Read more >
How to get the value of text input field using JavaScript
There is a text value property that can set and return the value of the value attribute of a text field. Also, we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found