-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
DisplayName not being forwarded when wrapped in #3438
Comments
Did you search for existing issues first?
…On Thu, 30 Jun 2022, 23:00 jtwigg, ***@***.***> wrote:
When I receive a printed call stack from React, the components wrapped in
observer show up as observerComponent
at observerComponent (http://localhost:3000/static/js/bundle.js:119423:69)
at div
at observerComponent (http://localhost:3000/static/js/bundle.js:119423:69)
at PrimaryWindow (http://localhost:3000/static/js/bundle.js:4346:5)
and in the callstack in the debugger shows up as anonymous
<anonymous> (/Users/johntwigg/development/crypto/loopNFT/ui/src/components/Cinema/CinemaMain.tsx:35)
<anonymous> (/Users/johntwigg/development/crypto/loopNFT/ui/node_modules/mobx-react-lite/src/observer.ts:104)
<anonymous> (/Users/johntwigg/development/crypto/loopNFT/ui/node_modules/mobx-react-lite/src/useObserver.ts:115)
*Intended outcome:*
Ideally, the underlying React Name would be maintained. CinemaMain in
this case.
*Actual outcome:*
anonymous and observerComponent
Looking at issue #3422 <#3422>
doesn't seem to resolve or change the behavior for me.
For example
const FocusLane : React.FC = observer(() => {...})
FocusLane.displayName = "FocusLane"
has no impact.
Any ideas? I'm open to workarounds.
*Versions*
➜ ui git:(mobx) ✗ npm list --depth 2 | grep mobx
├─┬ ***@***.***
│ ├── ***@***.***
—
Reply to this email directly, view it on GitHub
<#3438>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBFZQ2IROOO2ODL4GODVRYKG5ANCNFSM52KZEXGA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Yes. I referenced the one here
It claims to be fixed. Additionally, this describes behavior as "printing correctly" but its not, at least in the scenarios I described. |
Have you tried it without |
I can confirm that setting displayName on the return value of observe as imported from mobx-react-lite 3.4.0 doesn't work with the lasted react memo code. This code is now checking for name as well as displayName, and no longer forwards displayName changes if name is set, which is the case for observerComponent. Changed 15 months ago in: facebook/react@d1542de#diff-38bea54d83c3ea71b17acbd041cbe83ef053a57e54311943cb50268e03d06fcdR40. Also, it looks like if observerComponent were an anonymous functions, it would actually be replaced by displayName in the component stack trace built by react. https://github.com/facebook/react/blob/9abe745aa748271be170c67cc43b09f62ca5f2dc/packages/shared/ReactComponentStackFrame.js#L179 |
@mdeem thank you for the investigation. If I follow correctly, following could suffice? Object.defineProperty(observerComponent, "name", {
name: baseComponent.name
}) Or should we do this only |
Any idea how to write a test for this? If I const MemoCmp = React.memo(() => {
throw new Error('HERE');
return '';
}) The function name (second line) is just empty:
|
I think in our code we do return an anonymous function? https://github.com/mobxjs/mobx/blob/main/packages/mobx-react-lite/src/observer.ts#L103. However, iirc either babel or the js engine implicitly renames some anonymous functions based on the variable name they are assigned to. Maybe we can work around that by either using |
@mweststrate Redefining |
I just spent a good couple of hours debugging this problem, and here's what I've found. The biggest issue is that React doesn't respect the To work around this, as suggested above, you can instead redefine On top of all this, if you're using the "set the display name after defining the component" strategy (option 2 on what I consider the definite source on this, #141 (comment)), this still doesn't work. The reason for this is that you're changing the To work around this, I had to author the following dubious code:
Would it be possible to define |
Unless something changed, React propagates the displayName to inner component, but only if it doesn't have a mobx/packages/mobx-react-lite/src/observer.ts Lines 155 to 157 in cfcf3c6
|
If you look at the latest version React's As previously established in this thread, See https://github.com/facebook/react/blob/main/packages/react/src/ReactMemo.js#L50-L52 |
I see, well that settles my concern above - we will just respect the original component |
f239cb4 feedback welcome |
@urugator it doesn't work for me
|
IIRC the name/displayName handling of Object.defineProperty(memoObserverCmp, 'displayName', {
set(name) {
// Propagate name to created observerCmp
observerCmp.displayName = name;
// Modify original component name if it was inlined
if (!originalCmp.name && !originalCmp.displayName) {
originalCmp.displayName = name;
}
},
get() {
return observerCmp.displayName;
}
}) @abrindam Does that check out? |
For me, setting |
Afaik this issue is stale / no longer an issue with modern versions. If it is, please open a new issue |
When I receive a printed call stack from React, the components wrapped in
observer
show up asobserverComponent
and in the callstack in the debugger shows up as
anonymous
Intended outcome:
Ideally, the underlying React Name would be maintained.
CinemaMain
in this case.Actual outcome:
anonymous
andobserverComponent
Looking at issue #3422 doesn't seem to resolve or change the behavior for me.
For example
has no impact.
Any ideas? I'm open to workarounds.
Versions
The text was updated successfully, but these errors were encountered: