Friday, 27 May 2022

Next.js - TypeError: Cannot read properties of null (reading 'useMemo')

Since refactoring my code to move generic hooks and components to their own git submodules within my project I get TypeError: Cannot read properties of null (reading 'useMemo') whenever I call one of my custom hooks referring to useMemo.

I removed all the logic from a hook to make sure it didn't come from undefined arguments, so now my file looks like:

import { useMemo } from 'react'

export function useMyCustomHook() {
    return useMemo(() => [], []) // Does nothing useful, but fails anyway
}

export function useMyCustomHookWithoutMemo() {
    return [] // Does nothing useful, doesn't fail
}

I'm using next.js at the lastest version and the project structure is like this:

  • components/
    • component.js (this is where I call useMyCustomHook imported from 'generics')
  • hooks/
  • pages/
    • index.js (returns component)
  • generics/
    • index.js (with export * from './hooks/useMyCustomHook')
    • hooks/
      • useMyCustomHook.js

I also have a jsconfig.json file with the following content, so I can write stuff like import Component from 'components/component':

{
  "compilerOptions": {
    "baseUrl": "."
  }
}

Is next.js not compiling code in my generics folder? How can I get useMemo to work with this folder structure?



from Next.js - TypeError: Cannot read properties of null (reading 'useMemo')

Import error Symbol not found: _FSPathMakeRef

Can't import Quartz package. Every time I try it throws an error and the weird thing is the error disappears if I execute the same script with Sudo privileges (sudo python3 script.py). The problem started when I forcefully deleted the site-packages folder tried reinstalling python but didn't help.

Python version - 3.10.4

Mac version - Big Sur 11.6.5

>which python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
>sudo which python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3

Output:

Traceback (most recent call last):
  File "/Users/user/unknown/test.py", line 1, in <module>
    import Quartz
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Quartz/__init__.py", line 6, in <module>
    import AppKit
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/AppKit/__init__.py", line 10, in <module>
    import Foundation
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/Foundation/__init__.py", line 9, in <module>
    import CoreFoundation
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/CoreFoundation/__init__.py", line 9, in <module>
    import objc
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/__init__.py", line 6, in <module>
    from . import _objc
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so, 2): Symbol not found: _FSPathMakeRef
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/objc/_objc.cpython-310-darwin.so


from Import error Symbol not found: _FSPathMakeRef

Code duplication in API design for URL route functions vs. real world object methods

I have code duplication in my API design for the object methods vs. the URL routing functions:

# door_model.py

class Door:                               
    def open(self):                       # "Door.open" written once...
       ...
# http_api.py (the HTTP server is separated from the real-world object models)

@app.route('/api/door/open')              # ... written twice
def dooropen():                           # ... written three times
    d.open()                              # ... written four times!

d = Door()

How to avoid this unnecessary duplication of names in a similar API design? (while keeping a separation between real-world object models vs. HTTP server).

Is there a general pattern to avoid unnecessary duplication of names when using an object model (with methods), and URL routes functions? (nearly a Model View Controller pattern)

See also Associate methods of an object to API URL routes with Python Flask.



from Code duplication in API design for URL route functions vs. real world object methods