1

When I click the button the handler runs but it takes another click to actually push the state value into the array:

Here is my total code, with breaks of what I tried:

import { Button, Header, Message, Table, TextArea } from 'semantic-ui-react';
import React, { useState, useEffect, useCallback } from 'react';

export default function Day({ dayInfo }) {
  var [dayInfoInChild, setDayInfoInChild] = useState({});
  var [currentDate, setCurrentDate] = useState('');
  var [amountOfRows, setAmountOfRows] = useState(24);
  var [textValue, setTextValue] = useState('');
  var [textValueContainer, setTextValueContainer] = useState([]);

  function handleChange(e) {
    e.persist();
    setTextValue(e.target.value);
  }

  function handleClick(e, timeOfDayAndHour) {
    e.persist();
    e.preventDefault();

    setTextValueContainer((textValueContainer) => [
      ...textValueContainer,
      textValue,  /* <==== This should be adding from handleChange i.e. e.target.value into textContainer */
    ]);

    setDayInfoInChild({
      ...dayInfoInChild,
      [currentDate]: {
        [timeOfDayAndHour]: {
          message: textValueContainer,
        },
      },
    });
  }

  useEffect(() => {
    if (dayInfo !== null) {
      var modifiedDayInfo = dayInfo
        .split(' ')
        .map((item) => {
          if (item.indexOf(',')) return item.replace(/,/g, '');
        })
        .join('-');

      setCurrentDate(modifiedDayInfo);

      if (localStorage.getItem(modifiedDayInfo)) {
        var stringVersionOfModifiedDayInfo = modifiedDayInfo;

        modifiedDayInfo = JSON.parse(localStorage.getItem(modifiedDayInfo));

        if (!dayInfoInChild.hasOwnProperty(stringVersionOfModifiedDayInfo)) {
          setDayInfoInChild({
            ...dayInfoInChild,
            [stringVersionOfModifiedDayInfo]: modifiedDayInfo,
          });
        }
      } else {
        localStorage.setItem(modifiedDayInfo, JSON.stringify({}));
      }
    }
  }, [dayInfo]);

  useEffect(() => {
    return () => textValue; Because this value could change many times shouldn't this be correct?
  }, [textValue]);


  return (
    <>
     <h1>{dayInfo}</h1>
      <Table celled structured>
        <Table.Body>
          {Array.from(Array(amountOfRows)).map((row, index) => {
            return (
              <React.Fragment key={index}>
                <Table.Row>
                  <Table.Cell rowSpan="2" style={tableStyle}>
                    {TimeOfDaySetter(index)}
                  </Table.Cell>
                  <Table.Cell style={tableStyle}>
                    {
                      <strong>
                        {setExactHourHelper(index)}
                        :00
                      </strong>
                    }
                    <Message>
                      <Message.Header>Form data:</Message.Header>
                    </Message>
                    <TextArea
                      rows={2}
                      name="textarea"
                      onChange={(e) => handleChange(e)}
                      placeholder="Tell us more"
                    />
                    <Button
                      fluid
                      attached="bottom"
                      content="submit"
                      onClick={(e) => {
                        handleClick(
                          e,
                          `${setExactHourHelper(index)}-${
                            index < 12 ? 'AM' : 'PM'
                          }`
                        );
                      }}
                    />
                  </Table.Cell>
                </Table.Row>
                <Table.Row>
                  <Table.Cell style={(tableStyle, colorOveride)}>
                    {
                      <strong>
                        {setExactHourHelper(index)}
                        :30
                      </strong>
                    }
                    <Message>
                      <Message.Header>Form data:</Message.Header>
                      <pre></pre>
                    </Message>
                    <TextArea
                      rows={2}
                      name="textarea"
                      onChange={(e) => handleChange(e)}
                      placeholder="Tell us more"
                    />
                    <Button
                      fluid
                      attached="bottom"
                      content="submit"
                      onClick={handleClick}
                      // onKeyPress={this.handleKeyPress}
                    />
                  </Table.Cell>
                </Table.Row>
              </React.Fragment>
            );
          })}
        </Table.Body>
      </Table>
    </>
  );
}

So I've tried something like this, which on first blush seems to be the way to go... But then I get: TypeError: e.persist is not a function

useEffect(()=>{

  window.addEventListener('click', handleClick);

  return()=>{
    window.removeEventListener('click', handleClick);
 }
},[textValue])

Any help would be appreciated!

Update

This is a link to a codesandbox of my code...

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • @HermitCrab Thanks for the input! But that didn't work.... – Antonio Pavicevac-Ortiz Apr 14 '20 at 21:49
  • 1
    Can you please make a codesandbox with your code and give the link? – rzwnahmd Apr 14 '20 at 22:05
  • 2
    Sure! I'll update my question! – Antonio Pavicevac-Ortiz Apr 14 '20 at 22:23
  • What exactly are you trying to accomplish? It's not very clear to me based on the question. Are you just trying to render the "day planner" piece when you click a date and it isn't working? That's what I think you're trying to do but I just want to make sure – Michael Mayo Apr 15 '20 at 02:13
  • @MichaelMayo Essentially what started to do; when you click the calendar I’ve converted that date you get from it, and used that to create a key and object to stuff into local-storage to then create on the client. On the client I’m creating a collection of objects based on those dates with the each hour as keys in that object. Example “April-14-2020” : { “12:00AM”: “info you enter in the text area in form”}. So right now when I click the handler to push into textValueContainer, it doesn’t go in until the second click... – Antonio Pavicevac-Ortiz Apr 15 '20 at 02:30
  • Ok, that helps. When I initially opened the code sandbox I had to do some things to even get the calendar to work so I didn't know if that was the issue or not – Michael Mayo Apr 15 '20 at 02:33

0 Answers0