score:-1
You can append message in a variable and return that variable in renderMessage function
const renderMessages = (msgs) => {
let msgList = ""
console.log(msgs);
return msgs.forEach((msg, i) => {
msgList = msgList + "/n" + msg
setTimeout(() => {
return <Message>{msgList}</Message>;
}, 500);
});
};
/n will show next message in new line
score:-1
What does you program not do as expected? Do the messages not show?
If you want to render some element while iterating through an aray, i would advise to use array.map()
rather than array.forEach()
.
score:0
A functional component has to return the jsx to render. What you are trying to do does not work. In case of an array of messages you are returning msgs.forEach(...)
but Array.prototype.forEach
does not return an array but undefined
.
You will need to use state to queue up the messages one by one and render the contents of that state instead.
score:4
Here's an example of what could work for you: https://codesandbox.io/s/loop-through-array-with-react-d5tlc (I would suggest reviewing it for edge cases but the core functionality should be close to what you're looking for).
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const messages = ["fetching from sources...", "loading account..."];
const Loader = props => {
const { messages } = props;
// Default to the first message passed
const [messageIndex, setMessageIndex] = React.useState(0);
React.useEffect(() => {
// Move on to the next message every `n` milliseconds
let timeout;
if (messageIndex < messages.length - 1) {
timeout = setTimeout(() => setMessageIndex(messageIndex + 1), 1000);
}
return () => {
clearTimeout(timeout);
};
}, [messages, messageIndex]);
return <div>{messages[messageIndex]}</div>;
};
function App() {
return (
<div className="App">
<Loader messages={messages} />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Source: stackoverflow.com
Related Query
- react loop through array of strings with delay and set the text value
- How to loop through an object with key and associated array and filter the array based on value presence in another array
- How to set defaultValue, value={this.props.value}, and update the value of a text input with React?
- mapping an array of objects and changing the value with on onClick in React
- Loop through array of file urls and download each one with React
- loop through an array and show single item at a time with dynamic time duration like setTimeout or SetInterval in react js
- How to loop through an array of strings backwards x amount of times and start over when the end is reached?
- Loop through Array of object with the same data on some object and get it displayed once
- What is the correct approach in React to loop through an array to build html-select list of items in a function and call it in render()?
- React - Show an array value at a time and change with the click
- Iterate through a for loop with the help of react and read out the data record
- Loop through two array and based on condition change the property value not working
- SOLVED: Iterating through an array and selecting only the first value TS & React
- loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
- react js filter through an array of objects and compare values within to get the closest value to a variable
- how can i loop through the array inside of an object. and render it in the react component
- react css style syntax - set value of css prop with variables and text in one line
- React - Loop through an array of objects, and highlight the selected item onClick
- How to loop through an array and display data within the object of that array, in react and javascript
- React JS: Loop through a multi-dimensional array and determine/load the correct component
- Loop through an array of strings and render it as a Prop in a Component in React
- Loop through Object which has Array as value and create div for each key with its value as information
- Loop through an array in React with pause in between the iterations
- Updating a React Input text field with the value on onBlur event
- Test setting text value with React and Enzyme
- How to limit the text filed length with input type number in React JS and prevent entry of E,e, -, + etc
- Loop through array and render multiple instances of a child component with that data with React?
- React - Combine data returned two api calls and set state of an array with these values
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How to display and handle dynamic checkoxes that are dependent on Task array value in the backend (Mongodb) in react js?
More Query from same tag
- Inline access of a custom attribute in React
- child component cannot access function that is passed down with context hook
- IIS: Host 2 SPAs under the same site
- Jest/React/Redux error: Actions may not have an undefined "type" property. Have you misspelled a constant? Action: {}
- How do I check to make sure the form is filled on button click in React?
- how to properly format JSX in the new class (extends component) format
- cannot read property of 'map' undefined - typescript react
- Why dont works this Effect in React JS with context API?
- Rebase syncState does not update Firebase data
- React Router conditioning by Route
- react_on_rails install: yarn is required error
- How to convert react JSX files to simple JavaScript file [offline transformation]
- I can't implement Redirect in React
- npx create-react-app not working "Must use import to load ES Module:"
- Update an array element in React Hookstate
- Cannot get updated state value inside event handler
- Set props as state in child component
- Upload Kendo React PDF to Firebase storage
- How to dynamically update the components in ReactJS
- Convert class component to functional component in React
- Setting same value using set function in Record is creating a new object
- Child component sharing state with parent?
- Setting homepage in package.json doesn't set PUBLIC_URL on build
- How to fix 500 response in a Laravel project
- How to save cookies in browser using GO + REACT?
- Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong
- React htmlFor - clicking label focus on the wrong input
- what is the correct type for a React instance of a component in typescript
- setstate then response in rest api react
- In redux one reducer is changing other reducer