Next.js Discord

Discord Forum

Data not displayed in compenent

Unanswered
Swedish Lapphund posted this in #help-forum
Open in Discord
Swedish LapphundOP
So I am using my api client to receive data from the backend, currently before the return with my html code i use console.log() and can see my data being initialized properly.

105 Replies

Swedish LapphundOP
is in my browser, it shows the data I fetched from the server
  console.log(profileData);
  
  return (  
            <TextField
              label="Voornaam"
              value={profileData?.firstName}
              onChange={(e) => updateProfileData("firstName", e.target.value)}
              fullWidth
              margin="normal"
              disabled={!editMode}
            />
            <TextField
              label="Achternaam"
              value={profileData?.lastName}
              onChange={(e) => updateProfileData("lastName", e.target.value)}
              fullWidth
              margin="normal"
              disabled={!editMode}
            />
but it does not display on the page
I re-ran it to be sure
Swedish LapphundOP
here's all, not sure what could be it so I'll show all of it
if you need my states too let me know, though it shouldnt be an issue since it fetches properly
could you show the code for how you fetch profileData?
Swedish LapphundOP
export default function ViewProfile() {
  const [profileData, setProfileData] = useState<PanelProfileDto | undefined>();
  const [error, setError] = useState<ErrorResponse | undefined>(undefined);

  useEffect(() => {
    apiClient.panel.profile
      .get()
      .then((data) => {
        setProfileData(data);
      })
      .catch((error) => {
        if (error instanceof ErrorResponse) {
          console.log(error);
          setError(error);
        }
      });
  }, []);

  const updateProfileData = (field: any, value: any) => {
    setProfileData((prevData: any) => ({
      ...prevData,
      [field]: value,
    }));
  };
TextField is from mui?
Swedish LapphundOP
yepp
  async get() {
        return await this.http.get<PanelProfileDto>('/panel/profile');
    }
export interface PanelProfileDto {
    userid: string;
    profileId: string;
    firstName: string;
    lastName: string;
    email: string;
    phoneNumber: string;
    postalCode: string;
    specialDataId: string;
    created_at: string;
    updated_at: string;
    sensitiveData: SensitiveDataDto;
}

is my dto
@Swedish Lapphund ts async get() { return await this.http.get<PanelProfileDto>('/panel/profile'); }
do you see the value if you type in the input?
Swedish LapphundOP
yes
and it saves it too
I click edit, and then I can type and it saves it just fine
so changing the data works, for those two that is, the others dont work for other reasons im aware of
but none of them display the data I get from the server
@Swedish Lapphund but none of them display the data I get from the server
try this
 const [profileData, setProfileData] = useState<PanelProfileDto | undefined>({
   firstName: "",
   lastName: ""
 });
Swedish LapphundOP
I'd need to define them all, can do, one moment
Swedish LapphundOP
  const [profileData, setProfileData] = useState<PanelProfileDto | undefined>({
    firstName: "",
    lastName: "",
    userid: "",
    profileId: "ads",
    email: 'string',
    phoneNumber: 'string',
    postalCode: 'string',
    specialDataId: 'string',
    created_at: 'string',
    updated_at: "tring",
    sensitiveData: {     preferredInterviewType: "sd",
      preferredApproachType: 'string',
      commercialApproaches: true,
      assistanceTools: new Array<AssistanceToolDto>,
      availabilityMoments: new Array<AvailabilityMomentDto>,
      disabilities: new Array<DisabilityDto> }
  });
i got this now, it does not change anything sadly
still empty
Swedish LapphundOP
yes, just changed it back from the defaultValue
            <TextField
              label="Voornaam"
              value={profileData?.firstName}
              onChange={(e) => updateProfileData("firstName", e.target.value)}
              fullWidth
              margin="normal"
              disabled={!editMode}
            />
            <TextField
              label="Achternaam"
              value={profileData?.lastName}
              onChange={(e) => updateProfileData("lastName", e.target.value)}
              fullWidth
              margin="normal"
              disabled={!editMode}
            />
could it be because it's nullable?
const [profileData, setProfileData] = useState<PanelProfileDto | undefined>({
    firstName: "123",
    lastName: "",
    userid: "",
    profileId: "ads",
    email: 'string',
    phoneNumber: 'string',
    postalCode: 'string',
    specialDataId: 'string',
    created_at: 'string',
    updated_at: "tring",
    sensitiveData: {     preferredInterviewType: "sd",
      preferredApproachType: 'string',
      commercialApproaches: true,
      assistanceTools: new Array<AssistanceToolDto>,
      availabilityMoments: new Array<AvailabilityMomentDto>,
      disabilities: new Array<DisabilityDto> }
  });

do you see 123 appear in firstname field?
Swedish LapphundOP
YES
but scuffed
it does not move the label up
or try add this before the return
if (!profileData) return <div>loading...</div>
Swedish LapphundOP
why do i feel like I have to call onChange to make this work
@Ray or try add this before the return ts if (!profileData) return <div>loading...</div>
Swedish LapphundOP
done, does not change anything
Swedish LapphundOP
👀
Swedish LapphundOP
now its just empty like before
i could call onChange
in value
its a bit scuffed
it'd work probably
you can change the value when you type right?
Swedish LapphundOP
yes
my onChange works perfectly
its just the initial value
its almost as if MUI text field does not allow
a default value
the label is messed up if i dont type text myself
try changing one of the TextField to input
Swedish LapphundOP
the html tag input you mean?
yep
Swedish LapphundOP
kk sec
see if it works
Swedish LapphundOP
<input text={profileData?.firstName}></input>
like this?
@Swedish Lapphund `<input text={profileData?.firstName}></input>`
<input value={profileData?.firstName} onChange={(e) => updateProfileData("firstName", e.target.value)} />
Swedish LapphundOP
even that does not work
Swedish LapphundOP
this is right before the return statement
I can print it on the page too, let me try
            <p>{profileData?.firstName}</p>
            <input value={profileData?.firstName} onChange={(e) => updateProfileData("firstName", e.target.value)} />
its empty
👀
Swedish LapphundOP
but a console log right before the return
gives me
the data
<p>{JSON.stringify(profileData,null,2)}</p>
Swedish LapphundOP
its like its a different state or smth
try render this
Swedish LapphundOP
that works with my data
firstname
not firstName
:lol:
Swedish LapphundOP
klsdjfkds
broooooo aint no fucking way
my Dto has firstName
but we get it as a firstname
@Swedish Lapphund Click to see attachment
and lastname here too
Swedish LapphundOP
so it doesnt map
yepp
im so sorry mate for wasting your time, this is so stupid
my .get() does not throw an error when the json isnt mappe dproperly to my type
and I didnt notice
no worries:lolsob:
Swedish LapphundOP
any idea how I could solve this btw
when I click in it, it will put the label at the top, but when I leave the field itll overlap like this again
or try this
 <TextField
  label="Voornaam"
  value={profileData?.firstName || ''}
  onChange={(e) => updateProfileData("firstName", e.target.value)}
  fullWidth
  margin="normal"
  disabled={!editMode}
/>