React DOM not updating even though the state and props have

Rolo

I am looking to use map() to iterate over and add to my DOM in react, however, my DOM is not updating.

I think the everything is syntactically written ok, so I am really stumped by this. Any help you can give me I'd really appreciate, thanks.

App.js

class App extends Component {
...
    searchThis() {
        axios.get("/getYoutube", {params: {searchString: this.state.searchVal}}).then(({ data }) => {
            this.setState({
                youtubeVids: data.resp.items
            })
        })
    }

    render() {
        if (this.state.youtubeVids.length > 0) {
            console.log(this.state.youtubeVids[0].snippet.thumbnails.default.url)

            return (
                <div className="app-container">
                    <div className="app-header">
                        <h1>YouTube API App</h1>
                    </div>
                    <div className="app-body">
                        <div className="each-body-section">
                            <SearchResults 
                                youtubeVids = {this.state.youtubeVids}
                            />
                        </div>
                    </div>
                </div>
            );
        }
    }
}

export default App;

Right now I am bringing server api results into the frontend using setState() and Axios through the searchThis() method, which I am logging and seeing successfully in my console from my props. However in SearchResults.js, nothing is updating in the DOM SearchResults.js

import React from "react";

const SearchResults = props => {
    console.log("props in search ", props.youtubeVids)

    return (
    <div className="each-body-section video-container">
            {props.youtubeVids.map((video) => {
                <div key={video.id.videoId}>
                    <div className="video-title">
                        <h2>{video.snippet.title} </h2>
                    </div>
                    <div>
                        <img src={video.snippet.thumbnails.high.url} />
                    </div>
                    <div>
                        <p>{video.snippet.description}</p>
                    </div>
                </div>
            })}
    </div>
    )
}

export default SearchResults
Adam

Well, its because you are trying to map an empty array in SearchResults.js

Change SearchResults.js to the code below and try again:

import React, { Component } from 'react';

class SearchResults extends Component {

    render() {
        console.log(this.props.youtubeVids);
        if (this.props.youtubeVids.length > 0) {
            var youtubeVidsMap = this.props.youtubeVids.map((video) => <div key={video.id.videoId}>
                <div className="video-title">
                    <h2>{video.snippet.title} </h2>
                </div>
                <div>
                    <img src={video.snippet.thumbnails.high.url} />
                </div>
                <div>
                    <p>{video.snippet.description}</p>
                </div>
            </div>);

        }
        return (

            <div className="each-body-section video-container">
                {youtubeVidsMap}
            </div>
        );
    }
}

export default SearchResults;

If you want to use stateless component, the code below should work:

import React from "react";

const SearchResults = props => {

    if (props.youtubeVids.length > 0) {
        var youtubeVidsMap = props.youtubeVids.map((video) => <div key={video.videoId}>
            <div className="video-title">
                <h2>{video.snippet.title} </h2>
            </div>
            <div>
                <img src={video.snippet.thumbnails.high.url} />
            </div>
            <div>
                <p>{video.snippet.description}</p>
            </div>
        </div>);

    }

    return (
    <div className="each-body-section video-container">
            {youtubeVidsMap}
    </div>
    )
}

export default SearchResults

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redux state not updating even though reducer is called

React DOM not updating on state change

vueJs updating props even though i am method variable

React Dom not updating after updating a state array

React updating state leads to change in props

Updating state with props on React child component

React component state not updating with passed props

react hooks, cant updating state via props

Updating state on props change in React Form

React Redux: Getting Props And Updating State

Functions output is not updating after a minute even though it have to

React & Redux: State of component is not updating even with mapStateToProps

React : state not immediatly updating, even with prevState and useEffect

React updating props when I update the state that references to that props

Simple React Test Failing Even Though Correct Props Being Passed In

Old state still exist even after updating state in react

My Redux state is not updating even-though the old an new state are not equal

REACT, setInterval keeps returning NAN even though state is a number

React Hooks - useEffect fires even though the state did not change

React state is undefined , even though console.log shows it

Changes not showing even though React state successfully changes and component rerenders

Even though I filled the state in React, it looks empty in the render

React Hooks - Updating state using props without event handlers

React child component state not updating when new props are passed

Props not updating when redux state change in React Hooks

Simple Redux-React trying to updating state by this.props.dispatch

React shouldComponentUpdate() is called even when props or state for that component did not change

Laravel + Vue + Axios: DOM not updating from within Axios call, even though 'this' DOES update

React Component Not Being Populated even after updating state and executing forEach