The issue arose in the Relay QueryRenderer fragmentContainer as it received conflicting IDs from the passed props and the server

Experiencing a curious problem:

  1. Initiating a graphql request from QueryRenderer
  2. Obtaining response data from server (verified through Network tab in dev tools)
  3. Props being populated in the QueryRenderer render({ error, props }) function
  4. The props are passed down to child component using createFragmentContainer, which renders the value
  5. The displayed value for one field is different compared to the response

I'm uncertain about relay's behavior when retrieving data from its own store. I suspect it may be due to the absence of an id declaration in a type. Here are code examples:

App.js

<QueryRenderer
  environment={env}
  query={graphql`
    query ScoreboardContainerQuery($ID: ID!) {
      scoreboard(id: $ID) {
        ...Scoreboard_scoreboard
      }
    }
  `}
  variables={{ID: gameID}}

  render={({ error, props }) => {
    return <Scoreboard scoreboard={props ? props.scoreboard : undefined} />
  }}
/>

Scoreboard.js

const Scoreboard = ({ scoreboard }) => (
  <main>
    {scoreboard.matches.map(match => <Match key={match.id} match={match} />)}
  </main>
)

export default createFragmentContainer(Scoreboard, {
  scoreboard: graphql`
  fragment Scoreboard_scoreboard on FootballScoreboard {
    matches {
      ...Match_match
    }
  }
  `,
})

Match.js

const Match = ({ match }) => (
  <div>
    <div>
      {match.homeTeam.displayName}-
      {match.homeTeam.score}
    </div>
    <div>
      {match.awayTeam.displayName}-
      {match.awayTeam.score}
    </div>
  </div>
)

export default createFragmentContainer(Match, {
  match: graphql`
    fragment Match_match on Match {
      date
      homeTeam { // this is a Team type
        id
        displayName
        score
      }
      awayTeam { // this is a Team type
        id
        displayName
        score
      }
    }
  `,
})

Sample response for matches from server:

matches = [
  {
    "date": "2017-09-03T06:00:00Z",
    "homeTeam": {
      "id": "330",
      "displayName": "STG",
      "score": "20"
    },
    "awayTeam": {
      "id": "332",
      "displayName": "CBY",
      "score": "0"
    }
  },
  {
    "date": "2017-08-27T06:00:00Z",
    "homeTeam": {
      "id": "329",
      "displayName": "PEN",
      "score": "14"
    },
    "awayTeam": {
      "id": "330",
      "displayName": "STG",
      "score": "0"
    }
  },
  {
    "date": "2017-08-12T05:00:00Z",
    "homeTeam": {
      "id": "330",
      "displayName": "STG",
      "score": "42"
    },
    "awayTeam": {
      "id": "337",
      "displayName": "GCT",
      "score": "0"
    }
  },
]

Rendered values:

(
  <main>
    <div>
      <div>STG-42</div>
      <div>CBY-6</div>
    </div>
    <div>
      <div>PEN-0</div>
      <div>STG-42</div>
    </div>
    <div>
      <div>STG-42</div>
      <div>GCT-18</div>
    </div>
  </main>
)

Therefore, all occurrences of STG are replaced with 42, which should not happen.

Could this issue arise because there is no id in the Match type that results in an array response? Is Relay searching for a Team with the same id?

Answer №1

The reason for this issue is that Relay continuously updates the Team with ID 330 (which represents SGT) with the most recent value, and the final one in the sequence happens to be 42.

To address this, consider removing the score field from the Team. It may seem unusual for a team to have only one score; instead, you can introduce two separate fields within the Match type: awayTeamScore and homeTeamScore.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Eslint is actively monitoring files specified in the eslintignore to ensure they meet the set

I am currently working on a TypeScript project that is bundled by Webpack. I want to integrate Eslint into the project, but I have encountered an issue where Eslint watches compiled files even if they are listed in the .eslintignore file. .eslintignore: . ...

Creating an ngFor loop with an ngIf condition for true and false bot conditions

I am attempting to troubleshoot an issue where if my condition is true, return true. If my condition is false, return false. However, currently, if only one condition is true, all conditions are being applied as true. Please help me resolve this problem. ...

Why am I getting the error in react-dom.development.js related to my index.js file?

Upon checking the console, the following message is displayed: react-dom.development.js:86 Warning: ReactDOM.render is no longer supported in React 18. Please use createRoot instead. Until you transition to the new API, your application will behave as if i ...

How can the 'env' property in Nuxt.js be utilized to access the new API?

I have been working on creating news web apps that utilize newsapi.org. To protect my API key, I decided to use the env property in Nuxt.Js. However, I am now encountering a 401 status code from the server. Firstly, I created the .env file in my project d ...

Run a JavaScript function once the one before it has finished executing

I need help with executing a series of JavaScript functions in a specific order, where each function must wait for the previous one to complete. I've tried multiple approaches but haven't been successful so far. Any suggestions would be greatly a ...

Facing an ESIDIR error in NextJs, despite the fact that the code was sourced from the official documentation

For an upcoming interview, I decided to dive into learning Next.js by following the tutorial provided on Next.js official website. Everything was going smoothly until I reached this particular section that focused on implementing getStaticProps. Followin ...

What are the advantages of generating an HTML string on the server side and adding it to the client side?

Is there a more efficient method for transferring a large amount of data from the server to the client and dynamically generating tables, divs, and other HTML elements using JavaScript (jQuery)? Initially, I attempted to generate these elements on the cli ...

Tips for making Angular automatically generate the property in HTML when employing the bind feature

When using property binding in Angular, I noticed that the property is not created in the HTML unless I directly write the value in the code. For example, in a StackBlitz demonstration with a 'button' component that changes color based on the typ ...

Identifying the moment when attention shifts away from an element

Is it possible to detect when focus occurs outside an element without relying on global selectors like $(document), $(body), or $(window) for performance reasons? If achieving this without global selectors is not feasible, provide a provable reason expla ...

Using a for loop in Javascript with a specified increment value

I am working with an array containing 100 elements. My goal is to extract elements from the 10th to the 15th position (10, 11, 12, 13, 14, 15), then from the 20th to the 25th, followed by the 30th to the 35th, and so on in increments of 4, storing them in ...

Exploring the spinning globe using ThreeJS

Is there a way to rotate an object in three-dimensional space using THREEJs? I've been trying the code below but it doesn't seem to work. Can anyone spot what I might be doing incorrectly? object.matrixAutoUpdate = false; const matrix = object ...

Display the div in all browsers except for Internet Explorer

There's a message in a div that needs to be displayed on the webpage, but it's not showing up on Internet Explorer. Even after attempting the usual , as well as other recommendations, the message still remains hidden. What could I be overlooking ...

Drag and Drop in HTML5: Resize Div Automatically upon Transfer

I have implemented a drag and drop feature that transfers images into three different divs. However, I am facing an issue where the dimensions of the div do not adjust according to the image inserted, causing them to not fit in a single box. Additionally, ...

Interactive html5 canvas game with swiping mechanism

Currently, I am in the research phase for a new HTML5 canvas game that I want to create as a personal project to enhance my skills. Surprisingly, I have not been able to find any valuable articles or tutorials online about creating swipe-based games. The ...

Add a hovering effect to images by applying the absolute positioning property

On my React website, I am utilizing Tailwind CSS and I am attempting to showcase an image that has interactive elements. I want certain parts of the image to increase in size slightly when hovered over. My approach to achieving this was to save each part o ...

Tips for replacing all occurrences of a specific string in HTML while preserving JavaScript attributes

Is there a way to use RegEx to replace part of an HTML document without affecting the root element or other elements like event listeners? Consider this scenario: for (; document.body.innerHTML.indexOf(/ea/) != -1;) { document.body.innerHTML = docu ...

Sorting a list with anchor tags alphabetically using Javascript/JQuery

Here is a list of services: <ul id="demoOne" class="demo"> <li><a href='http://site/Service.aspx?shId=1154'>Copy service for files from folder 12</a></li> <li><a href='http://site/Service.aspx? ...

iOS Simulator offers a range of both offline and online events for testing purposes

I have been working on a phonegap 3.3 application that incorporates angularjs. When testing the application in my browser, I am successfully able to detect and respond to 'offline' and 'online' events. However, when I switch to the ios ...

What is the best way to access a particular property of an object?

Currently, I am successfully sending data to Mongo and storing user input information in the backend. In the console, an interceptor message confirms that the data is received from MongoDB. However, I am struggling to extract specific properties such as th ...

What is the best way to retrieve the canonicalized minimum and maximum values within the beforeShow method of jQuery UI's datepicker?

I have a pair of datepicker elements that I want to function as a range. When a value is set in the "low" datepicker, it should adjust the minDate and yearRange in the other datepicker, and vice versa with the "high" datepicker. The challenge I'm faci ...