Issue with Custom Input Component in React MDX #2553
-
I'm experiencing an issue with a custom input component in my React MDX setup. The input field, which is connected to a React hook for its value and onChange, only accepts one character at a time. I suspect this behavior is related to how the state is being updated and the component's rendering process. It seems that the component might be re-rendering in a way that causes the input to lose focus, preventing users from typing multiple characters. Has anyone encountered a similar issue or have any suggestions for resolving this? Any insights would be greatly appreciated! Page.tsx
TestRender.tsx
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi! There’s a lot of code here, things like CSS, and many tools (providers?), that are not needed to debug your problem. |
Beta Was this translation helpful? Give feedback.
Your issue is more of a React issue than an MDX issue. You’re defining the
FillInTheBlank
component inside youruseEffect
. That means as far as React is concerned, every time the dependency arrayuseEffect
is triggered, theFillInTheBlank
component is a new component, unrelated to the previous one.Your
useEffect()
should not depend onanswers
, while abiding to React’s rules of hooks. As long asanswers
is in the dependency array, or ESLint complains about it, the approach is wrong.To get a better mental model, I recommend you start by removing
useMdxComponents
. Then pass thecomponents
prop to<Content />
instead while rendering. It looks like your goal is to provide data from this comp…