Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use react hooks where ReactMarkdown is rendered #715

Closed
4 tasks done
karosowka13 opened this issue Dec 2, 2022 · 5 comments
Closed
4 tasks done

Cannot use react hooks where ReactMarkdown is rendered #715

karosowka13 opened this issue Dec 2, 2022 · 5 comments
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on

Comments

@karosowka13
Copy link

karosowka13 commented Dec 2, 2022

Initial checklist

Affected packages and versions

8.04

Link to runnable example

Full example can be see here.

Steps to reproduce

I want to set up sections menu based on headers that are in the markdown.
When I call set state in components parser I've got an error.

e.g.

const headerParser = ({ node, className, children, level, ...props }: any) => {
      if (children === undefined) {
        console.log("returnm empty block");
        return <></>;
      }
      let text = children[0]?.props?.children[0];
      let href = children[0]?.props?.href;
      let id = href === "#" ? href.slice(1) : href;
      setSections([...sections, {title:text, level}])
      return (
        <node.tagName {...props} {...{ className }} id={id}>
          <a href={href}>{text}</a>
        </node.tagName>
      );
}

When I move set state to parent component and add React.memo, it works. But I cannot reset the sections menu when the components rerender. It just adds more records to it from previous views, which is no a goal.

The same with any other hook like useEffect etc.

Expected behavior

The state should be change without infinite loop.

Actual behavior

Error

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

Runtime

Node v17

Package manager

npm 8

OS

macOS

Build and bundle tools

Webpack, Create React App

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Dec 2, 2022
@karosowka13 karosowka13 changed the title Cannot use react hooks when ReactMarkdown is rendered Cannot use react hooks where ReactMarkdown is rendered Dec 2, 2022
@aaronkarpati
Copy link

I experienced the same issue! The example attached shows the problem clearly❗️Seems like the component renderer and a component scoped state change/hook causes an infinite loop.

@Murderlon
Copy link
Member

Murderlon commented Dec 2, 2022

Hi, note that this is not an issue with react-markdown, you break React patterns by incorrectly setting state inside a render causing an infinite loop.

I'm guessing you probably want something like this (pseudo code, not tested):

import remark from 'remark'
import remarkHeadings from '@vcarl/remark-headings';

const headingProcessor = remark().use(remarkHeadings)

function Component ({content}) {
  const headings = useMemo(() => {
    const file = headingProcessor.processSync(content)
    return file.data.headings
  }, [content])
}

@karosowka13
Copy link
Author

Yeah you are definitely right.
Thank you very much for your answer!
I will close the issue.

@github-actions

This comment has been minimized.

@Murderlon Murderlon added 💪 phase/solved Post is done 🙋 no/question This does not need any changes and removed 🤞 phase/open Post is being triaged manually labels Dec 5, 2022
@github-actions

This comment was marked as resolved.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 💪 phase/solved Post is done labels Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

3 participants