Enter the previous script type

Uncertain about the type to assign here.

"HowitterObject" in setHowitters represents data and "...prev' is the ongoing accumulation of data from howitterObject.

 interface IhowitterMessage {
  message: string;
  createAt: number;
  id: string;
}

const Home = () => {
const [howitters, setHowitters] = useState<IhowitterMessage[]>([]);

  const getHowitters = async () => {
    const dbHowitter = await dbService.collection("howitter").get();
    dbHowitter.forEach((document) => {
      const howitterObject = {
        ...document.data(),
        id: document.id,
      };
      setHowitters((prev: ???) => [howitterObject, ...prev]); //Need guidance on what to put in the "prev" type?
    });
  };

  useEffect(() => {
    getHowitters();
  }, []);

  return(
    <div>
       {howitters.map((howitter: IhowitterMessage) => (
          <div key={howitter.id}>
            <h4>{howitter.message}</h4>
          </div>
        ))}
    </div>
   );
};

If you console.log(howitters), it will display as shown below.

(3) [{...}, {...}, {...}]
    0: {message: "no", createAt: 1631367025550, id: "q2d9TTgh36mgFZwMQ5EA"}
    1: {createAt: 1631365463319, message: "Good", id: "nCABFp1v3dP73gIbckpp"}
    2: {message: "hey", createAt: 1631367021665, id: "dmNBa6C8NwhDQDYDOP36"}

Answer №1

It closely resembles the nature of your state

Since prev represents your previous state of howitters, you can also include IhowitterMessage[] there:

setHowitters((prev: IhowitterMessage[]) =>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way for me to figure out if a Primefaces RadioCheckbox has been selected?

Despite the numerous examples available on how to count checked checkboxes, I am facing difficulties in getting it to work. My goal is to enable a button when at least one checkbox is checked on the page, and disable it when none are selected. However, n ...

What is the best way to access values from dynamically added components in Svelte when using data from a REST API in a loop?

Previously, I posted this query but now I've made changes to utilize a REST service for retrieving the item list. Everything functions as expected when the data is hardcoded, however, I encounter undefined values when using data from the REST service. ...

One effective way to redirect after a PUT request and display a one-time message

Here's what I'm aiming for in terms of desired behaviour: A user navigates to a password change web page. The user completes the form and it is sent via PUT request to our REST server. Upon successful completion, the user is redirected to their ...

Tips on saving Firebase Storage image url in Firebase database?

How do I store the URL of an image uploaded to Firebase Storage in Firebase Database? When executing the code below, I encounter the following error: Uncaught (in promise) FirebaseError: Function DocumentReference.set() called with invalid data. Unsuppor ...

Having trouble getting the Next.js Image component to work with Tailwind CSS

Recently, I've been working on transitioning a React project to Next.js and encountered some issues with the next/Image component that seem to be causing some problems. <div className=" flex flex-col items-center p-5 sm:justify-center sm:pt-9 ...

Getting the elements from multiple arrays in Python

It seems like a simple task, but I'm not quite sure how to tackle it. Let's say we have three different arrays: x = [1, 2, 3, 4, 5] y = [1, 3, 5, 7 ,9] z = [1, 5, 10, 15, 20] So, the question is: How can I loop through each element of these arr ...

Express Validator: The Art of Isolating Validation Logic

This query is more focused on structuring code rather than troubleshooting bugs or errors. I am currently tackling request body validation, where the JSON structure looks like this: { "title": "Beetlejuice", "year&qu ...

Is this loader going through a triple loop?

<style> .loader { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background-repeat: no-repeat; background: url('images/page-loader.gif'); } </style> <script src="//ajax.googleapis.com/ajax/libs/jque ...

Upon logging in to the first HTML page, the user will be automatically redirected to the second HTML page while passing values

Is there a way to automatically redirect users to Login2.html when selecting "TEAM" in the dropdown on Login1.html, using the values entered in Login1.html? Similarly, can we redirect them to the Premium page when they select "Premium"? I need a solution u ...

Refactor the fat arrow function in Typescript to maintain the bare function signature verification

When using AOT in Angular, it is necessary to rewrite all functions and reducers to not utilize arrow functions: An error occurred: Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function o ...

What is the right rendering strategy to use for shouldComponentUpdate in React?

Provide an exhaustive list of all props required for rendering shouldComponentUpdate(nextProps, nextState) { if (this.props.color !== nextProps.color) { return true; } if (this.state.count !== nextState.count) { return true; ...

Tips on populating a React table using an array of objects

I have encountered a simple problem that I haven't been able to solve yet. The first block of code seems to be working fine, but the second one is only producing empty table cells. Can someone explain why this is happening and provide a way to populat ...

Using JavaScript to toggle the visibility of grids depending on the radio button choice and then triggering this action by clicking on the search button

As a newcomer to AngularJS development, I am encountering some challenges while attempting to implement the following scenario. Any suggestions or guidance would be greatly appreciated. I aim to showcase either one or two Angular UI grids based on the rad ...

Open the .exe file using an HTML link in Firefox browser

While working on a project using php / js / html, I encountered the need to launch a C# application from my web page without requiring any downloads. The challenge was that I primarily use Mozilla Firefox and could only find solutions involving ActiveXOb ...

Swapping one div for another, all the while incorporating a limited number of buttons within the webpage

I'm faced with a dilemma on my HTML page. In the middle of the content, there's a div that contains 4 links with images and text inside. What I'm looking to achieve is for each link to trigger a complete change in the div when clicked, with ...

Performing mathematical computations through a mixin without relying on inline javascript code

Looking to enhance my web app with a mixin that shows a list of entries, each with a time stamp indicating how long ago it was posted. mixin listTitles(titles) each title in titles article.leaf article a.headline(href=title.URL)= title.t ...

End your Idp session and log out using passport-saml

Encountering a 400 bad request error when attempting to log out a user from the idp session. Despite successfully logging out the user from the application/passport session, they remain logged in to the idp session. The logout and callback endpoints are c ...

What is the proper way to initialize a two-dimensional array in JavaScript?

I recently received a suggestion on this question to create a two-dimensional array in my jQuery quiz application. However, I seem to be encountering an error while running the code. I suspect the issue might be due to incorrect formatting or a lack of ini ...

Tips on typing the onFocus function event parameter for a Material UI Input component

Currently, I am working on a custom dropdown using material ui components like Input and Popper. The goal is to have the popper open when the user focuses on the input field. Additionally, I am implementing this solution with TypeScript. import ClickAwayL ...

Enhancing user experience with AngularJS: Harnessing ng-Click for seamless task management on display pages

I'm struggling with my TodoList in AngularJS. I need help creating the ngClick function for the "addTodo" button. My goal is to send the entire form data to another page where I can display the tasks. Can someone guide me on what needs to be added to ...