question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React: <BuilderComponent> doesn't rerender when user attributes change

See original GitHub issue

We have a basic Builder component:

<BuilderComponent model="home-pages" />

If we call builder.setUserAttributes before this renders, it will pass the correct attributes to the Builder API and render the correct content.

If we then call builder.setUserAttributes after this renders (e.g. if the user signs in), it doesn’t show the new content.

Problem 1: Builder doesn’t fetch new content from the API. We can tell this is a problem because there’s nothing in the network tab when we call builder.setUserAttributes.

Problem 2: Builder fetches new content from the API, but the component doesn’t update. We can tell this is a problem because even if we manually fetch new content and pass it into <BuilderComponent>, it doesn’t update:

// doesn't work:
function MyComponent() {
  const [content, setContent] = useState(null)

  useEffect(() => {
    builder.get('home-pages').promise().then(setContent)
  })

  return <BuilderContent model="home-pages" content={content} />
}

// works:
function MyComponentWithKeyUpdate() {
  const [content, setContent] = useState(null)

  useEffect(() => {
    builder.get('home-pages').promise().then(setContent)
  })

  return <BuilderComponent model="home-pages" content={content} key={content.id} />
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
steve8708commented, May 6, 2020

btw we found a bug that with the last release with symbols not displaying in the editor, so when you upgrade I’d recommend using @builder.io/react@1.1.17 😃

will also release again if needed when we find what might be causing unsubscribing to not always work

1reaction
steve8708commented, May 5, 2020

Yeah @antonhalim it follows the ES Observable spec so you just do

const subscription = builder.userAttributesChanged.subscribe(incrementUserAttributesVersion)

// when you need to unsubscribe
subscribption.unsubscribe()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How and when to force a React component to re-render
The component did not change, so there was no re-rendering trigger. Here's why. React evaluates state changes by checking its shallow equality ...
Read more >
React: why child component doesn't update when prop changes
Update the child to have the attribute 'key' equal to the name. The component will re-render every time the key changes.
Read more >
When does React re-render components? - Felix Gerschau
Changing the state means that React triggers an update when we call the setState function (in React hooks, you would use useState )....
Read more >
React doesn't always trigger a re-render on setState
For new React developers, it is common to think that setState will trigger a re-render. But this is not always the case. If...
Read more >
React re-renders guide: everything, all at once - Developer way
When the value in Context Provider changes, all components that use this Context will re-render, even if they don't use the changed portion ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found