score:1
Accepted answer
If the cover needs to be retrieved, put it into state:
const Album = (props) => {
const [cover, setCover] = useState('');
Get the cover, if needed, when the component mounts:
useEffect(() => {
if (!props.album.thumbnail) {
getAlbumCover();
}
}, []);
// inside getAlbumCover:
.then(res => res.json())
.then(setCover)
.catch(handleError); // don't forget this; unhandled rejections should always be avoided
Then render it:
<img id={`img-${album.id}`} src={album.thumbnail || cover} />
Source: stackoverflow.com
Related Query
- How Do You Resolve Promises for Attributes of HTML Tags?
- How do you render HTML tags in React with Coffeescript?
- how to put data in single line for 2 header tags html
- How can you make a conditional external script reference in the public html file in reactjs. i.e. one for production and one for staging
- How to write inline styles for html tags in React?
- How do you set a HTML data- tag for a ReactJS TextField in a form
- How do I add an HTML page for template tags when I am using Routes to display web app pages
- How do you test for the non-existence of an element using jest and react-testing-library?
- React - How to pass HTML tags in props?
- How to add custom html attributes in JSX
- In Sublime Text 3, how do you enable Emmet for JSX files?
- How to query by text string which contains html tags using React Testing Library?
- How to correctly wrap few TD tags for JSXTransformer?
- How do you use React.js for SEO?
- How do you provide default props for nested shape in React?
- How do you reference a process.env variable in HTML <script src="" ? React
- How can one extend React types to support html attributes as props?
- How to extend HTML attributes in React with Typescript
- How do you write Jest tests for getInitialProps?
- How do you set the Typescript type for useRef hook in React Native?
- How to wait for setState in componentDidMount to resolve when testing with enzyme?
- How to resolve the no-undef error for setTimeout
- Regular expression for syntax highlighting attributes in HTML tag
- How do I get the TypeScript engine to allow custom HTML attributes in JSX?
- How promises inside for loop are working?
- How to resolve ':react-native-vector-icons' error for a successful build?
- How do I set custom fonts for header, body, and button tags for Material UI?
- In Sublime Text 3, how do you enable Autocompletion of HTML in JSX
- How do we return a Promise from a store.dispatch in Redux - saga so that we can wait for the resolve and then render in SSR?
- Jest: How to mock a promise on the same file for resolve and reject options?
More Query from same tag
- React Error : is declared but its value is never read. [6133]
- How to read only specific parts of the markdown files created through Netlify CMS in React dynamically?
- ClickAwayListener component not working with DragDropContext
- How to use animation every time image changes with react-spring?
- testing button class with enzyme
- How to send events from electron to react components only after all components are ready?
- How to submit the form in stepper
- Switching from class component to functional components
- Using Fetch with React Compnent
- Update state values with props change in reactjs
- How do you use state variables to make a nested drag and drop in React / Kanban board not working in React
- Send authorization request from React to Spring REST
- Why in the new react-router-dom <Redirect/> does not fire inside the setTimout?
- Why "TypeError: Cannot read property 'airline' of undefined" when it is defined?
- Got Received number of calls: 0 error for login functionality
- redux-sagas callback (aka sagas and setState)
- Uncontrolled input React Hooks ( console error)
- How do I get the decimal in the right place with mongoose-currency?
- React hook form - Register field not working
- cannot update context in react typescript functional component
- React should form elements be a separate component?
- get value from other react component
- How to remove className in ReactJS when page load?
- How to import just one function from different file into other?
- Use React componentDidMount in a modal/overlay react component
- Why is my react app not fetching my api data files?
- Getting typescript errors with any
- Webpack won't resolve modules without .jsx extension despite resolve being set
- Cannot delete data in database, despite sending just fine
- Nesting CSS selectors in material-ui?