Blazor - Compare Previous and Next State

自作多情 提交于 2020-06-15 07:10:08

问题


I have a table of students in Blazor, which is coming from an api, and I am also receiving a pushed data to update the students info which is basically the score upon a change in the database, the push is working fine and the score is being updated but I also want to change the background colour of the field that has been updated in the table upon a change in the score to red just the td tag for few sec, my code is as follow:

@foreach(var student in SS.GetStudents()){
     <tr>
          <td> student.name </>
          <td> student.section </>
          // trying to compare between the previous and next state
          var stud = SS.GetStuentsCopy().SingleOrDefault(s =>s.Id == student.Id);
          var color = "";
          if(stud.score != student.score){
            color = red;
           }
          <td class="@color"> student.score </>
     </tr>
  }

   @code{
         [Inject]
         public StudentsStates SS { get; set;}
         public StudentsResponse Students { get; set; }
         protected override async Task OnInitializedAsync()
         { 
              // Subscribe to the StateChanged EventHandler
               SS.StateChanged +=
               SSAdvancedStateChanged;

          Students = await Service.GetStudents();
         // update the students and the copy together
          SS.UpdateStudents(Students)
          SS.UpdateStudentsCopy(Students)


       //upon receiving students updated score
                hubConnection = new HubConnectionBuilder()
        .WithUrl(NavigationManager.ToAbsoluteUri("/studhub"))
        .Build();

        hubConnection.On<StudentsResponse>("ReceiveMessage", s =>
        {
            // update the students after 3 sec update the copy
            SS.UpdateStudents(s);


           //Here the state is not being updated
           // unless there is a new push 
           // or the issue might be in rendering
           // FYI without the sleep also I can see the changes in the color
            System.Threading.Thread.Sleep(3000);
            SS.UpdateStudentsCopy(s);

        }

      }}

StudentsStates.cs

namespace Ctrl.Web.Data
{
    public class StudentsStates
    {

        public StudentsResponse Students { get; set; }
        public StudentsResponse StudentsCopy { get; set; }
        public StudentsResponse GetStudents(){return Students;}
        public StudentsResponse GetStudentsCopy(){return StudentsCopy;}
        public void UpdateStudents(Students students){ Students = students;}
        public void UpdateStudentsCopy(Students students){ StudentsCopy = students;}

}}

As I said above everything is working fine except when there is a multiple push in a sec, the first pushed student score's background color is being changed too fast, sometimes u won't even notice it because of the pushed data and state is being updated, what I want is to slow down the background color without effecting the next pushed student score or if there is a better approach to this scenario, your answers are highly appreciated.


回答1:


I would suggest creating a component for the student row as follow:

@foreach(var student in SS.GetStudents())
{
      <StudentRow Student={student} />
}

Then inside the StudentRow component u can create a new private student variable where u update it after a delay of 3sec and do the comparison there, Instead of saving the ids in a list or in another copy:

StudentRow.razor

  <tr>
      <td> Student.name </>
      <td> Student.section </>
      var color = "";
      if(StudentCopy.score != Student.score){
        color = red;
       }
      <td class="@color"> student.score </>
 </tr>
 @code{
         public StudentResponse StudentCopy { get; set; }
         [Parameter]
         public StudentResponse Student { get; set; }

         protected override async Task OnParametersSetAsync()
         {
          await Task.Delay(3000);
          StudentCopy = Student;

         }
      }

OnParametersSetAsync method invoked when the component has received parameters from its parent in the render tree, and the incoming values have been assigned to properties. Read more here



来源:https://stackoverflow.com/questions/62087832/blazor-compare-previous-and-next-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!