Javascript 反应初始状态

Javascript 反应初始状态,javascript,reactjs,react-router-v4,Javascript,Reactjs,React Router V4,我需要在render()之前从服务器下载数据并用HTML覆盖它。我有错误this.state.news未索引。我尝试了getInitialState和componentWillMount的示例,但它不起作用 import { Component } from 'react' import { Link } from 'react-router-dom' import $ from 'jquery' export default class News extends Component{ co

我需要在render()之前从服务器下载数据并用HTML覆盖它。我有错误this.state.news未索引。我尝试了getInitialState和componentWillMount的示例,但它不起作用

import { Component } from 'react'

import { Link } from 'react-router-dom'
import $ from 'jquery'

export default class News extends Component{
constructor(props){
    super(props);
    this.state = { news: []};
}

componentWillMount() {
    const setState = this.setState;
    $.ajax({
        type: "GET",
        url: "localhost/news",
        data: {},
        success: function(data){
            setState(() => data);
        },
    });
}

render(){
    let news = this.state.news;
    return(
        news.map(function(n, i){
            return <div class="news-block">
                <Link to={`/news/post/${n.id}`}><h5>{n.theme}</h5></Link>
                <small>
                    <Link to={`/user/${n.author}`}>
                        {n.author}
                    </Link>, {n.date_of_create}
                </small>
                <p>{n.about}</p>
            </div>;
        })
    );
}
}

我认为你的代码有几个问题

第一个问题是,在处理异步数据时,您的行为就像以同步方式获取数据一样

另一个问题是,您正在访问您所在州的“news”属性,但它从未被分配

您应该尝试以下方法:

import { Component } from 'react'

import { Link } from 'react-router-dom'
import $ from 'jquery'

export default class News extends Component{
constructor(props){
    super(props);
    this.getNews = this.getNews.bind(this);
}

componentWillMount() {
    const setState = this.setState.bind(this);
    $.ajax({
        type: "GET",
        url: "localhost/news",
        data: {},
        success: function(data){
            setState(() => data);
        },
    });
}

render(){
    let news = this.state.news;
    return(
        news.map(function(n, i){
            return <div class="news-block">
                <Link to={`/news/post/${n.id}`}><h5>{n.theme}</h5></Link>
                <small>
                    <Link to={`/user/${n.author}`}>
                        {n.author}
                    </Link>, {n.date_of_create}
                </small>
                <p>{n.about}</p>
            </div>;
        })
    );
}
}
从'react'导入{Component}
从“react router dom”导入{Link}
从“jquery”导入$
导出默认类新闻扩展组件{
建造师(道具){
超级(道具);
this.getNews=this.getNews.bind(this);
}
组件willmount(){
const setState=this.setState.bind(this);
$.ajax({
键入:“获取”,
url:“本地主机/新闻”,
数据:{},
成功:功能(数据){
设置状态(()=>数据);
},
});
}
render(){
让新闻=this.state.news;
返回(
news.map(函数(n,i){
返回
{n.主题}
{n.作者}
,{n.date\u of\u create}
{n.about}

; }) ); } }
请用英语提问,或使用Try与此搭配:
const setState=this.setState.bind(this)
import { Component } from 'react'

import { Link } from 'react-router-dom'
import $ from 'jquery'

export default class News extends Component{
constructor(props){
    super(props);
    this.getNews = this.getNews.bind(this);
}

componentWillMount() {
    const setState = this.setState.bind(this);
    $.ajax({
        type: "GET",
        url: "localhost/news",
        data: {},
        success: function(data){
            setState(() => data);
        },
    });
}

render(){
    let news = this.state.news;
    return(
        news.map(function(n, i){
            return <div class="news-block">
                <Link to={`/news/post/${n.id}`}><h5>{n.theme}</h5></Link>
                <small>
                    <Link to={`/user/${n.author}`}>
                        {n.author}
                    </Link>, {n.date_of_create}
                </small>
                <p>{n.about}</p>
            </div>;
        })
    );
}
}