react 中用fetch取到的json数据 渲染不到页面中

使用了ant-design样式框架
取到的数据header是200状态码 ,respond也有数据,就是页面没有效果
fetch部分:

    componentWillMount(){
        var myFetchOptions={
            method :'GET'
        };
        fetch("http://v.juhe.cn/toutiao/index?type="+this.props.type +
            "&key=46e7985e9edd9368dde9136c8fae6", myFetchOptions)
        .then(response => response.json())
        .then(responseJson => this.setState({news:responseJson}));
        

    };

construtor函数:

constructor(){
            super();
            this.state={
                news:''
            }
        };

response中的数据:

clipboard.png

数据的渲染:

news.result.data.map((newsItem,index)=>(
            <div key={index} class="imageblock">
                <Link to={'details/${newsItem.uniquekey}'} target="_blank">
                <div class="custom-image">
                <img alt="" style={styleImage} src={newsItem.thumbnail_pic_s} />            
                </div>
                <div class="custom-card">
                    <h3 style={styleH3}>{newsItem.title}</h3>
                    <p>{newsItem.author_name}</p>
                </div>
                </Link>
            </div>
            ))    
阅读 4.6k
4 个回答

constructor 中的 news 初始化类型有误,应该是个空数组 [] 而不是空字符串 '',如果是空字符串的话,第一次渲染时 news.result.data.map 会报错,所以你的页面是个空白页

constructor(){
    super()
    this.state = {
        news: []
    }
};

看看render有没有走到

循环赋值的时候,不应该是this.state.news.result.data.map嘛?

新手上路,请多包涵

constructor里应该定义为{}空对象,在willMount方法和render里分别输出,看需要的数据到底有没有setState成功。

constructor(){

super()
this.state = {
    news: {}
}

};

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题