melkaya94
@melkaya94

Как пробросить данные через axios?

как пробросить данные из obj в CardBlock( obj состоит из двух массивов - feature и items, в CardBlock нужно пробросить items) ? Сейчас не работает, не могу понять почему

class IndexPage extends  Component{
    constructor(props){
        super(props);

        this.state = { 
            posts: null 
        };

    }
    componentWillMount(){ 
        axios.get('http://www.mocky.io/v2/5a0b3479320000a504e963b9')
            .then(res => {
                const posts = res.data; 
                this.setState({posts}); 
            })

            .catch(function (error) {
                console.log(error);
            });
    };

    render(){

        const obj = this.state.posts;
        console.log(obj);

        return (
            <div>
                 <CardBlock array = {obj}/>
            </div>
            )
    }
}
  • Вопрос задан
  • 210 просмотров
Решения вопроса 1
melkaya94
@melkaya94 Автор вопроса
Все, я разобралась
constructor(props){
        super(props);

        this.state = { 
            posts: null,
            date: null
        };

    }
    componentDidMount(){ 
        axios.get('http://www.mocky.io/v2/5a0b3479320000a504e963b9')
            .then(res => {
                const posts = res.data.items; 
                const date = res.data.feature;
                this.setState({posts, date}); 
            })
            .catch(function (error) {
                console.log(error);
            });
    };

    render(){

        const obj = this.state.posts;
        const slide = this.state.date;

        return (
            <div>
                             
                <CardBlock items = {obj || []} />
                <ShowMoreBlock/>
            </div>
            )
    }
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
gadfi
@gadfi
https://gamega.org
1 никогда не делайте запрос в componentWillMount! для этого есть componentDidMount
2 проверьте что приходит в CardBlock скорее всего проблема в нем
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы