即使状态和道具已经存在,React DOM也不会更新

时间:2019-06-17 06:59:10

标签: javascript reactjs react-dom react-state-management

我希望使用map()进行迭代并以响应方式添加到我的DOM中,但是,我的DOM没有更新。

我认为所有内容都在语法上写得很好,因此我对此感到非常困惑。谢谢您能给我的任何帮助。

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;

现在,我将通过setState()方法,使用searchThis()和Axios将服务器api结果带到前端,我正在props中登录并在控制台中成功看到了该方法。但是在SearchResults.js中,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

1 个答案:

答案 0 :(得分:0)

好吧,这是因为您试图在SearchResults.js中映射一个空数组

将SearchResults.js更改为以下代码,然后重试:

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;

如果要使用无状态组件,则下面的代码应该可以工作:

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