A
A
Adilet Kasymbaev2022-01-08 17:06:00
React
Adilet Kasymbaev, 2022-01-08 17:06:00

An incomprehensible error in Mobx, what to do with it??

61d99cb36baab190578634.png

import {makeAutoObservable} from "mobx"
import {PostService} from '../services/PostService'

class Posts {

    posts = []

    constructor() {
        makeAutoObservable(this)
    }

    static async getAllPosts() {
        const res = await PostService.get()
        const data = res.data

        this.posts = data
    }

}

export default new Posts();


import React, {useEffect, useState} from 'react';
import cl from './Adverts.module.css'
import Title from '../../UI/Title/Title'
import Line from "../../UI/Line/Line";
import CategoryItem from "../../UI/CategoryItem/CategoryItem";
import PostItem from "../../UI/PostItem/PostItem";
import ParameterField from "../../UI/ParameterField/ParameterField";
import Button from "../../UI/Button/Button";
import {observer} from 'mobx-react-lite'
import Posts from '../../../store/Posts'

const Adverts = observer(() => {

    useEffect(() => {
        Posts.getAllPosts()
    }, [])

    return (
        <div className={cl.wrapper}>
            <div className={cl.adverts_block}>
                <Title margin="15px">Категории</Title>
                <Line margin="30px"/>
                <div className={cl.categories}>
                    <CategoryItem/>
                </div>

                <Title margin="15px">Объявления</Title>
                <Line margin="30px"/>
                <div className={cl.posts}>

                    {Posts.posts.map((post) => {
                        return (
                            <PostItem>
                                <Title fontSize="20px" margin="10px">{post.title}</Title>
                                <div
                                    className={cl.img}
                                >
                                </div>
                                <Line margin="10px" color="#16b0b0"/>
                                <ParameterField type="location" value="Бишкек"/>
                                <ParameterField type="price" value={post.price + ' сом'}/>
                                <ParameterField type="category" value={post.category}/>
                                <ParameterField type="description" value={post.body}/>
                                <Button>Подробнее</Button>
                            </PostItem>
                        )
                    })}

                </div>
            </div>
        </div>
    );
});

export default Adverts;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-01-08
@Toasty

static async getAllPosts() {

remove static

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question