score:0
first of all you forgot the return statement:
rendergifs() {
return this.retrieveurls().then(function(results) {
...
}
but this won't solve anything as it is returning a promise
.
you need to save request results in the state and then map it:
constructor(props){
super(props);
this.state = { images: [] };
}
componentdidmount() {
this.rendergifs();
}
rendergifs() {
this.retrieveurls().then(function(results) {
console.log(results); //logs array of urls
this.statestate({ images: results });
});
}
render() {
return(
<div id="gif-div">
{
this.state.images.map((url, index) => (<img key={index} src={url} alt="" />);
}
</div>
)
}
score:2
the problem lies with the rendering function for the images and the way react does diffing between the previous and current state. since the fetch is asynchronous and the render is not, when the fetch is completed react doesn't know that it needs to re-render you component. there are ways to force a re-render in this case, but a better solution is to use the functionality that's already part of the shouldupdate
check.
your implementation might change to look something like the following:
class images extends component {
state = {
images: [],
error: null
}
componentdidmount() {
return this.retrieveimages()
.then(images => this.setstate({images}))
.catch(error => this.setstate({error}))
}
... rest of your class definition
render () {
return (
<div>
{this.state.images.map(image => <img src={image.url} />)}
</div>
)
}
}
i would also have some handling for bad results, missing key / values, etc. i hope that works for you, if not let me know! :)
score:3
this is the nature of asynchronous functions; you can't return a value from within a callback to the original call site. if you were to write:
const res = this.retrieveurls().then(function(results) {
return results;
});
you'd only be changing the resolution value of the promise. res
won't be assigned the value of results
, but rather it will be assigned the promise created by this.retrieveurls()
, and the only way to retrieve the value of a resolved promise is by attaching a .then
callback.
what you could do is this:
this.retrieveurls().then(results => {
this.setstate({ images: results });
});
now your internal state will be updated asynchronously, and your component will be told to re-render with the new data, which you can use in your render function by accessing the state.
note: i'm using an arrow function to create the callback in the above example, because otherwise this
won't be bound to the right context. alternatively, you could do the old that = this
trick, or use function#bind
.
Source: stackoverflow.com
Related Query
- Having trouble with React rendering a series of images from a map function
- How Use images from javascript array in react using filter and map function
- Unable to update a list with react when its value is being changed using the hook useState function from React TypeError: map is not a function
- Elements from map function in React is not rendering
- Having trouble calling function in parent from child in react
- What is the best way to define "KEY" in React when creating a list with map function from an array which containing no unique ID or fields?
- Render elements with a wrapper from a map function in react
- Rendering HTML from map function in React Context API render function
- how to use this map function in React to call url with images name
- I am having trouble with dispatching redux action from react Login component . I have tried to figure it out in vain
- Having trouble rendering images from data given by the api Reactjs/DjangoRest
- React not rendering background images from function call
- Render Content Dynamically from an array map function in React Native
- Prevent react component from rendering twice when using redux with componentWillMount
- How to render images with React JS using map or loop?
- Optimizing React Rendering with Function Partial Application
- Uploading images from react with laravel api
- Map function with Graphql, React
- How to randomly make appear one by one images from a map with fade?
- map function in react with arrow function
- Testing in React with preloadedState: redux store gets out of sync when having a manual import of store in a function
- Rendering multiple buttons with onClick function in React
- React Hooks - having trouble with splice updating state during onClick event
- React images caption error from api maybe to do with correct Index
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- Use data from async function on React State with JSX
- Jest: having trouble keeping a reference of a mock function with `jest.fn()`
- how to get images from mongo with axios and display them with react
- Trouble with sending data from react to node
- Having trouble updating a React component using a Hook with a timer
More Query from same tag
- fecth id and date from url as pros in react js
- How to Display an Elements Property
- Redirecting with state and template literals react
- Joi validation react .custom() validation in react
- How to get access to redux props wit condtional rendering
- setting state based on conditional in reactjs
- React - fade in div, pause and fade out div
- React - dynamically create nav from components on page
- how to update the data in firebase database, instead of creating a new data, using reactjs?
- Dynamic css animation variable with React
- Async initial state React-Redux Redux-Thunk
- nav disappearing components using react hooks and react router
- React JS map function
- How to enable multiple check an enable unCheck in radio button?
- How to add HTML component to React.js
- ReactJS Router - .push() , how to stop execution after one?
- Unable to connect to mosquitto broker server in mqtt-react
- Calculate SubTotal in React-Redux
- how to fix warning with React and babel: Parsing error: Unexpected token <
- How does React Redux handle successive changes in a loop
- React Hooks Too many re-renders in pagination with redux
- Running into problems using React Datepicker with rails
- Customize `::-webkit-scrollbar` inline with React
- React how not to display if doing calculation inside jsx
- How to sort an array based on one property and then group by another property in React Redux application
- Node FetchError: invalid json response body - unexpected token < in JSON
- How can I convert React code to ClojureScript one?
- console.log showing data but getting error in if condition javascript (using inside react component)
- .innerText of an element is not showing up
- How do I create polar charts with highcharts-react-official?