微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 MobX 和 React js 在页面加载中加载数据

如何解决无法使用 MobX 和 React js 在页面加载中加载数据

我是使用 react.js 的 MobX 新手,我很高兴能探索更多有关 react 的信息。我在谷歌上搜索了很多,并在以下链接的帮助下尝试了示例,但没有运气。

Enabling decorators - MobX

有人可以介入并帮助我吗?我将此作为 POC 来启动我的项目。这是一个阻滞剂,对我来说意义重大

model.ts

  Private Sub Bw_DoWork(sender As Object,e As DoWorkEventArgs)

        Dim d = DirectCast(e.Argument,BwData).Path
        Dim s = DirectCast(e.Argument,BwData).Start

        Dim reader As IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(d)
        Dim l As String
        Dim lineIndex As Integer = 0
        Dim linestart As Integer = 0
        Dim part As String() = nothing

        Try
            Using ctx = New ContentsDBEntities

                Dim rowN = s 'Math.Max(ctx.xHamster.Count - 100,1)

                Do
                    l = reader.ReadLine
                    If l Is nothing Then
                        Exit Do
                    End If
                    part = l.Split("|")

                    If Bw.CancellationPending Then Exit Do

                    If lineIndex >= rowN AndAlso part.Length >= 14 Then
                        If lineIndex = rowN Then
                            linestart = lineIndex
                            startAt = Now
                        End If

                        Dim vId = part(0)

                        If IsNumeric(vId) Then
                            Dim there = (From c In ctx.xHamster Where c.VideoId = vId Select c).FirstOrDefault

                            If there Is nothing Then
                                Dim r = New xHamster With {
                                            .VideoId = vId,.Url = part(1),.Umbed_Url = part(2),.Title = part(3),.Duration = StringToSec(part(4)),.dateAdded = Date.Parse(part(5)),.Thumb = part(6),.Channels = part(7),.Pornstars = part(8),.MaxResolution = part(9),.Orientation = part(10),.Title_RU = part(11),.Username = part(12),.Description = part(13)}

                                ctx.xHamster.Add(r)

                                If lineIndex Mod 100 = 0 Then
                                    ctx.SaveChanges()
                                End If
                            End If
                        End If
                    End If
                    lineIndex += 1

                    DirectCast(sender,BackgroundWorker).ReportProgress(lineIndex,linestart)

                Loop Until l Is nothing

                reader.Close()
                ctx.SaveChanges()
            End Using

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub

agent.ts

export interface IActivity {
    id: string;
    title: string;
    description: string;
    category: string;
    date: string;
    city: string;
    venue: string;
}

activityStore.ts

import axios,{ AxiosResponse } from 'axios';

axios.defaults.baseURL = 'https://localhost:xxx/api';

const responseBody = (response: AxiosResponse) => response.data;
const requests = { get: (url: string) => axios.get(url).then(responseBody) };
const Activities = { list: () => requests.get('/activities/getactivities') };

// eslint-disable-next-line import/no-anonymous-default-export
export default { Activities };

App.tsx

import { observable,action,makeObservable } from 'mobx';
import { createContext } from 'react';
import { IActivity } from './../models/agent';
import agent from '../api/agent';

class ActivityStore {
    @observable activities: IActivity[] = [];

    constructor() {
        makeObservable(this);
    }

    @action loadActivities = () => {
        agent.Activities.list().then((response) => {
            this.activities.push(response);
        });
    };
}

export const activityStoreContext = createContext(new ActivityStore());

package.json

enter image description here

边缘浏览器的输出(在 chrome 和 firefox 中也相同)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。