[英]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. 总而言之,我有一个包含一些项目的菜单。
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. 我希望能够选择该菜单项,并在发生这种情况时,打开它旁边的一个窗体,该窗体有输入,并且如果有保存的值,这些输入将被预先填充。
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.