Decide

joined 1 year ago
[–] Decide@programming.dev 1 points 1 year ago (4 children)

If there's any links, resources, mental models, or anything that you or anyone else think would be helpful in getting this to work, I'm all ears. Also, since it's pretty obvious that this is an assignment, my limitation is that I cannot use useEffect, and the PhoneComponent has to use 4 inputs.

I've been stuck on this for about a week now, so any help, feedback, insight, or articles I should read would be incredibly appreciated.

[–] Decide@programming.dev 2 points 1 year ago* (last edited 1 year ago)

Hey, I'm just now seeing this. So, my component hierarchy is something like this:

App

  • Form
    • TextInput
    • PhoneInput

The TextInput components are very simple:

import { ErrorMessage } from "../ErrorMessage"; //this function can be used to determine if the error message renders based on criteria

export const FunctionalTextInput = ({
  dataProperty,
  errorMessage,
  placeholder,
  value,
  propertyHandler,
}: {
  dataProperty: string;
  errorMessage: string;
  placeholder: string;
  value: string;
  propertyHandler: (property: string, e: string) => void;
}) => {
  //Object.keys(initialUserData)[0]
  return (
    <>
      <div>
        {dataProperty}:
         propertyHandler(dataProperty, e.target.value)}
        />
      </div>
      
    
  );
};
export const FunctionalTextInput = ({
  dataProperty,
  errorMessage,
  placeholder,
  value,
  propertyHandler,
}: {
  dataProperty: string;
  errorMessage: string;
  placeholder: string;
  value: string;
  propertyHandler: (property: string, e: string) => void;
}) => {
  //Object.keys(initialUserData)[0]
  return (
    &lt;>
      <div>
        {dataProperty}:
         propertyHandler(dataProperty, e.target.value)}
        />
      </div>
      
    
  );
};

The shape of my data is like so:

export type UserInformation = {
  firstName: string;
  lastName: string;
  email: string;
  city: string;
  phone: string[];
};

In my Form Component, I have two functions that work in the TextInput component, but not the PhoneInput component.

const dataHandler = (e: FormEvent) => {
    e.preventDefault();
    userDataHandler(formData);
    setFormData(initialUserData);
  };
const propertyHandler = (property: string, value: string) => {
    setFormData((prevProp) => ({ ...prevProp, [property]: value }));
  };

So, over the past few hours I've been trying to talk to bing about this, and get some answers. After a few hours, I finally think the problem is a conflict of state. It seems like the state I'm using in my PhoneInput component interferes with the state of the parent component. This seems to be the case since when I click submit, my dataHandler function doesn't trigger for the PhoneInput component.

So, I guess now I'm wondering how that works? I've heard of raising state to the parent, but passing state down, not as data, but as actual state, sounds difficult and somewhat complex. I'm wondering how to use this technique, the uses, and how I can determine when to use it. Or, better yet, maybe I'm missing something and the answer is right outside my reach.

The phone input in question:

// This is a component that is used for the phone input
// it wall accept 4 inputs, and "lift" the values to the parent component as a single, unformatted string.

import { ChangeEventHandler, useRef, useState } from "react";
import { ErrorMessage } from "../ErrorMessage";

type TPhoneInputProps = {
  errorMessage: string;
  dataProperty: string;
  higherPhoneState: string[];
  propertyHandler: (property: string, e: string) => void;
};
export const FunctionalPhoneInput = ({
  errorMessage,
  dataProperty,
  higherPhoneState,
  propertyHandler,
}: TPhoneInputProps) => {
  const [phoneState, setPhoneState] = useState(["", "", "", ""]);
  const phoneNumber = [
    useRef(null),
    useRef(null),
    useRef(null),
    useRef(null),
  ];
  const phoneNum0 = phoneNumber[0];
  const phoneNum1 = phoneNumber[1];
  const phoneNum2 = phoneNumber[2];
  const phoneNum3 = phoneNumber[3];
  const phoneChangeController =
    (
      index: 0 | 1 | 2 | 3 //  1 | 2 | 3 | 4,
    ): ChangeEventHandler =>
    (e: React.ChangeEvent) => {
      const length = [2, 2, 2, 1];
      const nextInput = phoneNumber[index + 1];
      const prevInput = phoneNumber[index - 1];
      const maxLength = length[index];
      const value = e.target.value;
      const shouldGoToNextInput =
        maxLength === value.length &amp;&amp; nextInput?.current;
      const shouldGoToPrevInput = value.length === 0;
      const newState = phoneState.map((phone, phoneIndex) =>
        index === phoneIndex ? e.target.value : phone
      );
      if (shouldGoToNextInput) {
        nextInput.current?.focus();
      }
      if (shouldGoToPrevInput) {
        prevInput.current?.focus();
      }
      setPhoneState(newState);
      console.log(newState.join(""));
      console.log(dataProperty);

      // Concatenate the new state with e.target.value to get the full phone number
      // const fullPhoneNumber =
      //   newState.slice(0, index).join("") +
      //   e.target.value +
      //   newState.slice(index + 1).join("");
      propertyHandler(dataProperty, newState.join(""));
    };
  return (
&lt;> 
<div>
        Phone:
        <div>
          
          -
          
          -
          
          -
          
        </div>
      </div>

  );
};

Please note that this component is 1000% broken. I was in the process of changing it with Bings suggestions, but it's frustrating getting anything useful out of the thing.

[–] Decide@programming.dev 4 points 1 year ago

I've used it to help me understand some code concepts and debugging, but over the last two weeks, it went from competent to completely stupid half of the time. It also fails to connect so often, it's unreal.

[–] Decide@programming.dev 1 points 1 year ago (1 children)

Thank you 🙏

[–] Decide@programming.dev 1 points 1 year ago (3 children)

If you still have any, I'd appreciate one.

[–] Decide@programming.dev 1 points 1 year ago

Thank you! Your reply provides a lot of missing context. It will be my bible, or at least part of the guidelines I need to get something started.

[–] Decide@programming.dev 1 points 1 year ago (2 children)

This makes a lot of sense. I'll have to see how I can apply this locally, but at least it's more information that I had before on how to approach this. I appreciate it, thank you.

[–] Decide@programming.dev 4 points 1 year ago

Especially if it's free. Who would say no to free food?

[–] Decide@programming.dev 9 points 1 year ago

Similar, but in a past job, I didn't pick up on some cues, treated a coworker like how I treat everyone, and I got yelled at by them. They told me to respect the hierarchy and that I'm not his equal - I was training for a higher position at the time. I was later let go for other reasons they wouldn't mention.

[–] Decide@programming.dev 3 points 1 year ago

This is my third time writing this. I just have to say, I agree with you 100%. I've only begun to begin to act "normal" by being treated like shit and being afraid of being myself.

I've even had a boss tell me to only talk about the weather. It's grueling, dehumanizing seeing other people talk like normal, but when I do it, it's somehow wrong.

[–] Decide@programming.dev 1 points 1 year ago

Thanks for the tip! I've gotta try that

[–] Decide@programming.dev 1 points 1 year ago (2 children)

What's in your sauce? I've had soba before, I'll eat it without hesitation, but I feel like the standard sauce I get from the store is lacking, somewhat.

view more: ‹ prev next ›