reactjs - 如何在另一个类组件中使用 material-ui lab - checkboxestag 组件?
问题描述
我有一个包含主窗体的 Add.js。我有一个 CheckboxesTags.js 是一个函数组件。我需要在 Add.js 中获取 CheckboxesTags.js 的选定项目,以通过 axios 发送其他值。我添加了<CheckboxesTags onChange={(e)=>{console.log(e.currentTarget);}} />
,但我无法获得价值。
添加.js:
import React, { Component } from 'react';
import CheckboxesTags from './CheckboxesTags'
class Add extends Component {
constructor() {
super();
}
render() {
return (
<div >
<CheckboxesTags onChange={(e)=>{console.log(this.props);}} />
</div>
)
}
}
export default Add;
CheckboxesTags.js 正是 material-ui 代码:
import React from 'react';
import axios from 'axios'
import Checkbox from '@material-ui/core/Checkbox';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank';
import CheckBoxIcon from '@material-ui/icons/CheckBox';
const icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;
export default function CheckboxesTags() {
return (
<Autocomplete
multiple
id="checkboxes-tags-demo"
onChange={(e)=>{console.log(e.currentTarget);}}
options={optins}
disableCloseOnSelect
getOptionLabel={option => option.name}
renderOption={(option, { selected }) => (
<React.Fragment>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.name}
</React.Fragment>
)}
style={{ width: 500 }}
renderInput={params => (
<TextField {...params} variant="outlined" label="Options" placeholder="Favorites" />
)}
/>
);
}
let optins = [
];
axios.get('http://localhost:5000/api/products/notes').then(response => {
optins = response.data
})
解决方案
本质上,您需要从父组件向下传递单击处理程序。像这样的东西(未测试):
import React, { Component } from 'react';
import CheckboxesTags from './CheckboxesTags';
class Add extends Component {
constructor(props) {
super(props);
}
handleTagClick = tag => console.log(tag);
render() {
return (
<div>
<CheckboxesTags onSelect={this.handleTagClick} />
</div>
);
}
}
export default Add;
import React from 'react';
import axios from 'axios';
import Checkbox from '@material-ui/core/Checkbox';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank';
import CheckBoxIcon from '@material-ui/icons/CheckBox';
const icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;
export default function CheckboxesTags(props) {
const { onSelect } = props;
return (
<Autocomplete
multiple
id="checkboxes-tags-demo"
onChange={(event, value) => {
onSelect(value);
}}
options={optins}
disableCloseOnSelect
getOptionLabel={option => option.name}
renderOption={(option, { selected }) => (
<React.Fragment>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.name}
</React.Fragment>
)}
style={{ width: 500 }}
renderInput={params => (
<TextField
{...params}
variant="outlined"
label="Options"
placeholder="Favorites"
/>
)}
/>
);
}
由于您已经将onChange
事件附加到Autocomplete
元素,这将触发其中包含的任何代码。在这种情况下,它将是传递下来的onSelect
函数。
推荐阅读
- javascript - 循环只迭代一次 | JS
- java - 如何在 Kotlin Ktor 中运行 Cron 作业?
- postgresql - 使用 pgadmin 4 在 postgresql 中导入问题
- python - 如何将 ssd_mobilenet_v1_COCO_2017_11_17 加载到本地的 object_detection_webcam?
- python - 如何将此python代码转换为飞镖?
- flutter - 在 Flutter(Dart) 中访问实例对象中的数据
- image - 如何相对于 div 元素将图像放在彼此之上?
- ios - CGContextDrawImage 线程安全吗?
- python-3.x - Python:无法从 https 站点请求 html
- keycloak - 作为首次代理登录的一部分,有没有办法在 Keycloak 领域的本地数据库中禁用用户注册?