【问题标题】:Prefilling a <Create> record with render <Button/> cause the application to crash (react-admin)使用渲染 <Button/> 预填充 <Create> 记录会导致应用程序崩溃(react-admin)
【发布时间】:2019-08-16 12:32:59
【问题描述】:

早上好。我正在使用 react-admin (2.9.4) 并遇到问题: 我尝试预填充 Create 记录并使用自定义 工具栏 显示 Create 组件,但如果我为此使用 location.search,我的应用程序会崩溃。

我使用与示例中相同的结构

....
component={Link}
to={{
  pathname: '/comments/create',
  search: '?id=' + record.id,
}}
....

我的创建组件如下所示:

const ToolBar = (props) => (
  <Toolbar {...props}>
    <SaveButton />
    <Button />
  </Toolbar>
)


const MyCreate = props => (
  <Create {...props}>
    <SimpleForm toolbar={<ToolBar/>}>
      <TextInput source="title" />
      <TextInput source="summary" />
    </SimpleForm>
  </Create>
);

在这种组合中,我得到 出了点问题,并且警告主要是关于无法识别的道具,例如基本路径

index.js:1437 Warning: React does not recognize the `basePath` prop on a DOM element.

如果我在工具提示中注释掉 Button 就可以了。 如果有人告诉我可能是什么问题,以及 location.search 如何影响 Button 的呈现,我将不胜感激。

非常感谢。

【问题讨论】:

    标签: javascript reactjs react-admin


    【解决方案1】:

    “预填充记录” https://marmelab.com/react-admin/CreateEdit.html

    创建表单尝试获取给定格式的初始参数:

    const CreateRelatedCommentButton = ({ record }) => (
        <Button
            component={Link}
            to={{
                pathname: '/comments/create',
                state: { record: { post_id: record.id } },
            }}
        >
            Write a comment for that post
        </Button>
    );
    

    如果你想用你的搜索线来传递参数,你可以试试这个:

    import { stringify } from 'query-string'
    
    let search = stringify({
      myparams: JSON.stringify(
        {
          id: record.id, 
        })
    })
    

    要删除此警告,您需要实现按钮组件:

    // ToolbarButton.js
    
    import React from 'react'
    import PropTypes from 'prop-types'
    import classnames from 'classnames'
    import { Button } from 'react-admin'
    import { withStyles, createStyles } from '@material-ui/core'
    
    const styles = createStyles({
      button: {
        marginTop: '0em', 
      },
      iconPaddingStyle: {
        marginRight: '0.5em',
      },
    })
    
    const sanitizeRestProps = ({
      basePath,
      invalid,
      pristine,
      record,
      saving,
      submitOnEnter,
      handleSubmit,
      handleSubmitWithRedirect,
      ...rest
    }) => rest
    
    const ToolbarButton = ({ className, classes = {}, icon, ...rest }) => (
      <Button
        size="large"
        className={classnames(classes.button, className)}
        {...sanitizeRestProps(rest)}
      >
        { icon ? React.cloneElement(icon, { className: classes.iconPaddingStyle }) : null }
      </Button>
    )    
    

    【讨论】:

    • 感谢您的回答。但是,我不需要制作传输参数,因为 react-admin 从参数中获取 id 并像记录字段一样使用它。因此,当我重定向到 Create 时,我会使用我的 id 获取记录,然后我只需填写剩余的字段并使用我的 id 保存对象。但是,在这种情况下,如果在工具栏中使用 Button,我会收到错误(我在一个问题中提到过)
    • 谢谢,它对我有用。但是,请你澄清一下这一刻。为什么如果我们使用 locations.search 按钮只能在 sanitizeProps 之后工作?
    • 据我了解,locations.search 中的值最终出现在底层 HTML 元素的属性中,这导致:“TypeError:无法将对象转换为原始值”
    • 有趣,是不是这个问题。再次感谢。
    猜你喜欢
    • 2017-05-20
    • 2012-09-22
    • 2023-02-03
    • 2014-05-11
    • 2020-05-26
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多