更新已编辑输入的值

2024-04-05

我在用react-admin框架,我正在尝试动态更新我的输入值。

在我的自定义组件中,我有onChange()方法如下所示:

onChange = (value) => {
    this.setState({ currentForm: this.props.record });
    const { updated_src, name, namespace, new_value, existing_value } = value;
    this.setState({ currentForm: updated_src });
 }

首先我设置状态currentForm具有存储在的原始未编辑值this.props.record。之后我设置状态具有新的值updated_src。该变量存储具有新编辑值的对象。两个对象this.props.record and updated_src有相同的钥匙。

后来在render()我有这个字段:

{this.props.record && <JsonInput src={this.props.record} name={null} displayDataTypes={false} displayObjectSize={false} onEdit={this.onChange} />}

但是如果我这样做console.log(this.state.currentForm); in the onChange()方法,它返回一个空数组,而不是具有更新的字段值的对象。

我的整个组件:

import React, { Component, Fragment } from 'react';
import { fetchUtils, CardActions, EditButton, Button, List, Datagrid, Edit } from 'react-admin';
import Drawer from '@material-ui/core/Drawer';
import JsonInput from 'react-json-view';
import { Field } from 'redux-form';

import EditIcon from '@material-ui/icons/Edit';
import IconKeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';
import { SimpleForm } from 'ra-ui-materialui/lib/form';

const divStyle = {
 width: '400px',
 margin: '1em'
};

export default class JsonEditButton extends Component {
 constructor(props, context) {
  super(props, context);
  this.state = { showPanel: false , currentForm: []};
 }

  onChange = (value) => {
    //that works
    this.setState({ currentForm: this.props.record }, () => {
      const { updated_src, name, namespace, new_value, existing_value } = value;
      /* sets the updated values to state */
      this.setState({ currentForm: value.updated_src });
    });
  }

  handleClick = () => {
    this.setState({ showPanel: true });
  };

  handleCloseClick = () => {
    this.setState({ showPanel: false });
  };

 render() {
  const { showPanel } = this.state;

  return (
    <div>
      <Button label="Upravit JSON" onClick={this.handleClick}>
        <EditIcon />
      </Button>

      <Fragment>
        <Drawer
          anchor="right"
          open={showPanel}
          onClose={this.handleCloseClick}
        >
          <div>
            <Button label="Zavřít" onClick={this.handleCloseClick}>
              <IconKeyboardArrowRight />
            </Button>
          </div>
          <div style={divStyle}>
              {this.props.record && <JsonInput src={this.props.record} name={null} displayDataTypes={false} onKeyPressUpdate={true} displayObjectSize={false} onEdit={this.onChange} />}
          </div>
        </Drawer>
      </Fragment>
    </div>
  );
 }
};

有什么想法为什么这段代码不起作用以及如何解决这个问题吗?

先感谢您。


onChange = (value) => {
  this.setState({ currentForm: this.props.record },()=>{
  console.log("---currentForm------ >",
    this.state.currentForm,this.props.record)
  this.callFn(value)
});

}
callFn = (value) => {
  const { updated_src, name, namespace, new_value, existing_value } = value;
  this.setState({ currentForm: updated_src },()=>{
  console.log("---->newData",this.state.currentForm,updated_src) 
});
}

尝试这种方式,我认为它应该有帮助, 只需检查这个控制台,您就会得到,为什么您的阵列没有更新

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新已编辑输入的值 的相关文章

随机推荐