***来自建议的代码更新****** 我正在学习使用material-ui。我找不到将其与事件处理相结合的许多示例。我使用了自动完成功能和文本字段来创建建议的从API提取的数据列表。我可以呈现选定的列表,但是单击选择之一后,我无法获得单击的值传递给react类的成员函数。我需要将事件正确绑定到自动完成功能吗?我应该怎么做。我的代码中的第25行将事件目标记录到控制台,但始终为0(我假设为null)。如何将this.state.data的值设置为clicked选项?
我尝试添加autoSelect = {true}
我还尝试将这行代码移到textarea中。
onChange = {this.updateState}
import React from "react"
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: null,
isLoaded: false,
itemSelected: false,
inputVal: ''}
this.updateState = this.updateState.bind(this)
};
updateState(e) {
e.persist()
const newValue = e.target.value
this.setState({inputVal: newValue, itemSelected: true});
console.log(e.target.value);
// eventually I want to render a DIV with data from the selected value
}
/// fetch some data
componentDidMount() {
fetch('http://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
/* .then(json => console.log(json)) */
.then(data => this.setState({data, isLoaded: true}));
}
render() {
const {isLoaded, itemSelected} = this.state;
if (!isLoaded) {
return <div> loading ...</div>;
} else if (itemSelected) {
return <div> item selected </div>
} else {
const limo = this.state.data;
return (
<div>
<Autocomplete
freeSolo
disableClearable
autoSelect={true}
id = "limoSelect"
onChange={this.updateState}
value = {this.state.inputVal}
options={limo.map(option => "body: '" + option.body + '\n' + "' id: " + option.id)}
renderInput={params => (
<TextField
{...params}
label="Type In Content"
id="limoText"
value = ''
autoSelect={true}
margin="normal"
variant="outlined"
fullWidth
InputProps={{...params.InputProps, type: 'search'}}
/>
)}
/>
</div>
);
}
}
}
App.defaultProps = {}
export default App;
控制台记录为零。 当您单击选项时,将调用updateState并设置此变量 this.state.itemSelected = true; 没有错误信息。 我希望可以通过updateState中的console.log来记录单击的项目!
答案 0 :(得分:2)
onChange签名:函数(事件:对象,值:任意)=>无效
这是一个例子:
import React from 'react';
import Chip from '@material-ui/core/Chip';
import Autocomplete from '@material-ui/lab/Autocomplete';
import TextField from '@material-ui/core/TextField';
export default class Tags extends React.Component {
constructor(props) {
super(props);
this.state = {
tags: []
};
this.onTagsChange = this.onTagsChange.bind(this);
}
onTagsChange = (event, values) => {
this.setState({
tags: values
}, () => {
// This will output an array of objects
// given by Autocompelte options property.
console.log(this.state.tags);
});
}
render() {
return (
<div style={{ width: 500 }}>
<Autocomplete
multiple
options={top100Films}
getOptionLabel={option => option.title}
defaultValue={[top100Films[13]]}
onChange={this.onTagsChange}
renderInput={params => (
<TextField
{...params}
variant="standard"
label="Multiple values"
placeholder="Favorites"
margin="normal"
fullWidth
/>
)}
/>
</div>
);
}
}
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: 'Pulp Fiction', year: 1994 },
{ title: 'The Lord of the Rings: The Return of the King', year: 2003 },
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
{ title: 'Fight Club', year: 1999 },
{ title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
{ title: 'Star Wars: Episode V - The Empire Strikes Back', year: 1980 },
{ title: 'Forrest Gump', year: 1994 },
{ title: 'Inception', year: 2010 },
];
答案 1 :(得分:0)
编辑:使用e.target.textContent
可以解决问题。
这是一个实时Codesandbox,用于检查代码(修改了某些部分,应用了下面的技巧以及其他一些东西)。
this.state.itemSelected = true
使用setState(就像您已经在处理其他状态项一样)
updateState(e) {
this.setState({ inputVal: e.target.value, itemSelected: true });
console.log(e.target.value);
// eventually I want to render a DIV with data from the selected value
}
还有一个技巧,您可以使用数组解构:
const {isLoaded, itemSelected} = this.state;
代替
var isloaded = this.state.isLoaded;
var itemSelected = this.state.itemSelected;