简体   繁体   English

我如何动态传递值以响应形式

[英]how do i pass values dynamically in react to form

I am learning react by myself, and I am having a hard time doing something thought it would be simple. 我正在学习自己的反应,并且很难做一些认为很简单的事情。

In summary, I have a menu with a few items. 总而言之,我有一个包含一些项目的菜单。

  1. I want to be able to select that menu item and when that happens, open a form next to it, the form has inputs, and those input will be prefilled in case there's a saved value for it. 我希望能够选择该菜单项,并在发生这种情况时,打开它旁边的一个窗体,该窗体有输入,并且如果有保存的值,这些输入将被预先填充。

  2. I want, if possible, to hide the editable form in case I click away from the form. 如果可能,我想隐藏可编辑的表单,以防我单击离开表单。

I am not sure how to do that. 我不确定该怎么做。 I have been playing with the props, and react is complaining about uncontrollable and controllable components. 我一直在玩道具,而反应是抱怨无法控制和可控制的组件。 I read about it and I get and. 我读到它,然后得到。 Now I am not sure what is the best way to do this. 现在,我不确定什么是最好的方法。 I don't need a "hack" in case my solution is not the right way to do it. 万一我的解决方案不是正确的解决方案,我就不需要“ hack”。 I am really looking for how people would handle similar problem in an elegant way in React. 我真的在寻找人们如何在React中以一种优雅的方式处理类似的问题。

Here's parts of the code I was writing, using material-ui-next 这是我使用material-ui-next编写的部分代码

class EditMenu extends React.Component {
  constructor(props) {
    super(props);
    console.log(props);

    const itemsInfo = [
      {id: 11 , 
       title: 'title 1',
       description: 'desc 1'
      },
      {id: 22 , 
        title: 'title 2',
        description: 'desc 2'
       },
       {id: 33 , 
        title: 'title 3',
        description: 'desc 3'
       },
    ];

    let itemId = this.props.selectedItem;
    let item = _.find(itemsInfo, {id:itemId});

    this.state = {
      value: '',
      item: item,
      itemName: '',
      itemDescription: ''
    };
  }

  handleitemNameSetting = (event) => {
    event.preventDefault();
    debugger;
    this.setState({
        itemName: event.target.value
    });
  }

  render() {

    return (

      <div className="form-container">
        <form  >

          <TextField
            id="item-name"
            label="item Name"
            margin="normal"
            onChange={this.handleItemNameSetting}
            value={this.state.item.title}
          />
          <br />
          <TextField
            id="dish-desc"
            label="item Description"
            margin="normal"
            value={this.state.item.description}
          />
          <br />
          <TextField
            className="value-field-container"
            label="value"
            type="number"
            hinttext="item value" />

      </form>
        </div>
    );
  }
}


class MenuList extends React.Component {
    state = { editMenuOpen: false };

    handleClick = (id,event, item, ind) => {
      this.setState({editMenuOpen: true, selectedItem: id});
    };

    render() {
      const { classes } = this.props;
      const menuItems =  [
            {id: 11 , 
                title: 'title 1'
            },
            {id: 22 , 
                title: 'title 2'
            },
            {id: 33 , 
                title: 'title 3'
            },
         ];

      return (
          <div className={classes.root}>
              <Grid container spacing={24}>
                  <Grid item xs={2}>
                      <div>
                          <List
                              component="nav"
                              subheader={<ListSubheader component="div">Lunch Menu</ListSubheader>}
                              >

                              {menuItems.map(item => (
                              <ListItem button key={`${item.id}`} onClick= { () => this.handleClick(item.id)}>
                                  <ListItemText primary={`${item.title}`} />
                              </ListItem>
                              ))} 

                          </List>
                      </div>
                  </Grid>
                  <Grid item xs>
                      <div>
                         { this.state.editMenuOpen ? <EditMenu selectedItem={this.state.selectedItem}></EditMenu> : null }
                      </div>
                  </Grid>
              </Grid>

          </div>
      );
    }
  }

I would add an onClick event handler on the outermost parent element, that triggers a setState and changes the editMenuOpen to false. 我将在最外面的父元素上添加一个onClick事件处理程序,该事件处理程序将触发setState并将editMenuOpen更改为false。

Let's assume it's called handleCloseMenuClick . 假设它称为handleCloseMenuClick

But i would also need to add an event handler on my form itself that stops the event from handleCloseMenuClick from triggering when i click my form using event.stopPropagation() 但是我还需要在表单本身上添加一个事件处理程序,以在我使用event.stopPropagation()单击表单时阻止handleCloseMenuClick触发事件

Check the console in this example and see the difference when you remove the event.stopPropagation() 在此示例中检查控制台,并查看删除事件时的区别event.stopPropagation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript将表单值传递给表单中的其他字段 - How do I pass form values to other fields in a form with javascript 如何使用锚标记将表单值传递给操作? - How do I pass form values to an action using anchor tag? 如何将Infowindow值映射到HTML输入表单 - How do I pass maps Infowindow values to HTML input form 如何根据 React 中导入的数据传递不同的值? - How do I pass different values depending on the imported data in React? 我如何使用反应钩子将数据值传递给模态? - How do i pass data values to modals using react hooks? 如何将动态创建的表单字段的值传递到文本区域 - how to pass values of dynamically created form fields to a textarea 我如何以角形式传递动态生成的形式 - How can i pass a dynamically generated form in angular form 如何将值数组动态传递给<select>联系表 7 中的字段? - How to dynamically pass an array of values to a <select> field in Contact Form 7? 动态导入后如何将 props 传递给 React 组件? - How do you pass props to a React component after importing it dynamically? 在序列化并提交表单之前,如何将值从一个函数传递给另一个函数 - How do I pass values from one function to another before I serialize and submit the form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM