-3

I'm still learning react, so this may just be because I don't fully understand what I'm doing yet. The formattedText field is just a temporary text that the user creates from a button click. I want to create a running history of this field, so I created historyText.

function App() {
  const [formattedText, setFormattedText] = useState([]);
  const [historyText, setHistoryText] = useState([]);

In my button click action, I'm setting the formattedText:

setFormattedText(tempComp);

And I thought I could just append it to my history:

setHistoryText(historyText+tempComp);

But it doesn't work, all I get is [object Object],[object Object],[object Object],[object Object],[object Object] as my output.

** Clarification: the reason this may look strange because I'm storing html in the strings and wanted it to show as html. Here's how I'm building tempComp:

tempComp.push(<p><b>{dResult[y].dp5}</b></p>);

Different lines are formatted differently, so I couldn't just use a split to add in a <p> for example.

baumli
  • 381
  • 1
  • 19

1 Answers1

-1

This happens because, ou are not using String values but an Object and the default string representation of an object is just [object Object]. I'm not totally sure, what you want to add to read more here.

setHistoryText(historyText+tempComp);

but maybe try to console.log the objects out first, and see if maybe you where meant to access an attribute instead

//other logic
console.log(historyText) //do you mean to access a value on the object instead?
console.log(tempComp)
setHistoryText(historyText+tempComp);
Dedi
  • 2,812
  • 3
  • 22
  • 55