Tutorial throws context is undefined error in SaveContext

旧巷老猫 提交于 2021-01-04 09:15:51

问题


This may be a newbie problem, but I'm working though react-admin/Tutorial.html and when it comes to using the EditGuesser the 'Edit' page fails, reporting 'TypeError: context is undefined'. AFAIK, I've followed the instructions.

The error is reported after making the adjustment to introduce the 'edit={EditGuesser}' change in the 'Adding Creation and Editing Capabilities' section. I can see that the tutorial code has been edited, as the const definition of the dataProvider has been removed. Has something else also been removed?

If the error page is closed then the underlying page just states 'Something went wrong. A client error occurred and your request couldn't be completed.'

Any help is gratefully received.

The error details are:

TypeError: context is undefined
useSaveContext
prj_dir/node_modules/ra-core/esm/controller/details/SaveContext.js:23
  20 |  */
  21 | export var useSaveContext = function (props) {
  22 |     var context = useContext(SaveContext);
> 23 |     if (!context.save || !context.setOnFailure) {
  24 |         /**
  25 |          * The element isn't inside a <SaveContextProvider>
  26 |          * To avoid breakage in that case, fallback to props

which is kicked off from:

SaveButton
prj_dir/node_modules/ra-ui-materialui/esm/button/SaveButton.js:75
  72 | var notify = useNotify();
  73 | var translate = useTranslate();
  74 | var setOnSave = useContext(FormContext).setOnSave;
> 75 | var _d = useSaveContext(props), setOnSuccess = _d.setOnSuccess, setOnFailure = _d.setOnFailure, setTransform = _d.setTransform;
     | ^  76 | var handleClick = function (event) {
  77 |     // deprecated: use onSuccess and transform instead of onSave
  78 |     if (typeof onSave === 'function') {

回答1:


It seems to be a problem with the EditGuesser. If you have inserted the code dump for PostEdit in your posts.js, you just need to replace EditGuesser with PostEdit:

// in src/App.js    
- import { Admin, Resource, EditGuesser } from 'react-admin';
+ import { Admin, Resource } from 'react-admin';
- import { PostList} from './posts';
+ import { PostList, PostEdit} from './posts';
import { UserList } from './users';

const App = () => (
  <Admin dataProvider={dataProvider}>    
  -  <Resource name="posts" list={PostList} edit={EditGuesser} />
  +  <Resource name="posts" list={PostList} edit={PostEdit} />
     <Resource name="users" list={UserList} />
  </Admin>
);



回答2:


TypeError: Cannot read property 'save' of undefined

    at SaveButton (http://localhost:3000/static/js/0.chunk.js:118252:25)
    at div
    at div
    at Toolbar (http://localhost:3000/static/js/0.chunk.js:31209:23)
    at WithStyles(ForwardRef(Toolbar)) (http://localhost:3000/static/js/0.chunk.js:41101:31)
    at Toolbar (http://localhost:3000/static/js/0.chunk.js:124820:38)
    at WithWidth(Toolbar) (http://localhost:3000/static/js/0.chunk.js:37766:90)
    at form
    at SimpleFormView (http://localhost:3000/static/js/0.chunk.js:123872:21)
    at FormView (http://localhost:3000/static/js/0.chunk.js:106041:19)
    at ReactFinalForm (http://localhost:3000/static/js/0.chunk.js:171720:20)
    at FormWithRedirect (http://localhost:3000/static/js/0.chunk.js:105938:18)
    at SimpleForm
    at div
    at Paper (http://localhost:3000/static/js/0.chunk.js:20136:23)
    at WithStyles(ForwardRef(Paper)) (http://localhost:3000/static/js/0.chunk.js:41101:31)
    at Card (http://localhost:3000/static/js/0.chunk.js:4992:23)
    at WithStyles(ForwardRef(Card)) (http://localhost:3000/static/js/0.chunk.js:41101:31)
    at div
    at div
    at EditView (http://localhost:3000/static/js/0.chunk.js:119522:23)
    at EditViewGuesser (http://localhost:3000/static/js/0.chunk.js:119433:22)
    at EditGuesser (http://localhost:3000/static/js/0.chunk.js:119455:164)
    at WithPermissions (http://localhost:3000/static/js/0.chunk.js:92487:23)
    at Route (http://localhost:3000/static/js/0.chunk.js:175704:29)
    at Switch (http://localhost:3000/static/js/0.chunk.js:175906:29)
    at ResourceContextProvider (http://localhost:3000/static/js/0.chunk.js:100762:21)
    at ResourceRoutes (http://localhost:3000/static/js/0.chunk.js:100630:17)
    at Resource (http://localhost:3000/static/js/0.chunk.js:100700:15)
    at Route (http://localhost:3000/static/js/0.chunk.js:175704:29)
    at Switch (http://localhost:3000/static/js/0.chunk.js:175906:29)
    at RoutesWithLayout (http://localhost:3000/static/js/0.chunk.js:100806:21)
    at div
    at main
    at div
    at div
    at LayoutWithoutTheme (http://localhost:3000/static/js/0.chunk.js:131812:24)
    at WithStyles(LayoutWithoutTheme) (http://localhost:3000/static/js/0.chunk.js:41101:31)
    at withRouter(WithStyles(LayoutWithoutTheme)) (http://localhost:3000/static/js/0.chunk.js:175961:37)
    at Connect(withRouter(WithStyles(LayoutWithoutTheme))) (http://localhost:3000/static/js/0.chunk.js:172862:75)
    at ThemeProvider (http://localhost:3000/static/js/0.chunk.js:39803:24)
    at Layout (http://localhost:3000/static/js/0.chunk.js:131929:26)
    at Route (http://localhost:3000/static/js/0.chunk.js:175704:29)
    at Switch (http://localhost:3000/static/js/0.chunk.js:175906:29)
    at div
    at CoreAdminRouter (http://localhost:3000/static/js/0.chunk.js:100273:87)
    at Route (http://localhost:3000/static/js/0.chunk.js:175704:29)
    at Switch (http://localhost:3000/static/js/0.chunk.js:175906:29)
    at CoreAdminUI (http://localhost:3000/static/js/0.chunk.js:100482:15)
    at AdminUI
    at Router (http://localhost:3000/static/js/0.chunk.js:175339:30)
    at ConnectedRouter (http://localhost:3000/static/js/0.chunk.js:58988:7)
    at ConnectedRouterWithContext (http://localhost:3000/static/js/0.chunk.js:59093:25)
    at Connect(ConnectedRouterWithContext) (http://localhost:3000/static/js/0.chunk.js:172862:75)
    at TranslationProvider (http://localhost:3000/static/js/0.chunk.js:107638:28)
    at Provider (http://localhost:3000/static/js/0.chunk.js:172575:20)
    at CoreAdminContext (http://localhost:3000/static/js/0.chunk.js:100042:25)
    at AdminContext
    at Admin (http://localhost:3000/static/js/0.chunk.js:138349:22)
    at div
    at App



回答3:


The EditGuesser is broken in the latest react-admin version (3.11). A fix is incoming in 3.11.2 (https://github.com/marmelab/react-admin/pull/5728). In the meantime, please use version 3.10.4.



来源:https://stackoverflow.com/questions/65304121/tutorial-throws-context-is-undefined-error-in-savecontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!