reactjs - 无法将输入数据从 react-autosuggest 传递到父文件
问题描述
Hye,我是 React 的新手,我尝试从 react-autosuggest 组件获取输入到父文件:
父组件搜索栏:
import React, { Component } from 'react';
import './Searchbar.css';
import SearchButtons from './buttons/SearchButtons';
import SearchInput from './input/AutoSuggestInput';
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = {
inputValue: '',
};
}
getValue = (newValue) => {
this.setState({ inputValue: newValue });
}
render() {
return (
<div className="searchBarItems">
<SearchInput getValue={(event, value) => this.getValue(event, value)} />
<SearchButtons />
</div>
);
}
}
export default SearchBar;
具有自动建议功能的子组件 SearchInput:
import React, { Component } from 'react';
import './AutoSuggestInput.css';
import Autosuggest from 'react-autosuggest';
import cities from '../../../../assets/citiesDB/cities';
function escapeRegexCharacters(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function getSuggestions(value) {
const escapedValue = escapeRegexCharacters(value.trim());
if (escapedValue === '') {
return [];
}
const regex = new RegExp(escapedValue, 'i');
return cities.filter((city) => regex.test(city.name));
}
function getSuggestionValue(suggestion) {
return (suggestion.name);
}
function renderSuggestion(suggestion) {
return (
<span>{suggestion.name}</span>
);
}
class SearchInput extends Component {
constructor() {
super();
this.state = {
value: '',
suggestions: [],
};
}
onChange = (event, { newValue }) => {
this.setState({
value: newValue,
});
this.props.getValue(event, newValue);
};
onSuggestionsFetchRequested = ({ value }) => {
this.setState({
suggestions: getSuggestions(value),
});
};
onSuggestionsClearRequested = () => {
this.setState({
suggestions: [],
});
};
render() {
const { value, suggestions } = this.state;
const inputProps = {
placeholder: 'Saisir une ville',
value,
onChange: this.onChange,
};
return (
<Autosuggest
suggestions={suggestions}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
getSuggestionValue={getSuggestionValue}
renderSuggestion={renderSuggestion}
inputProps={inputProps}
/>
);
}
}
export default SearchInput;
我有两个问题:
src\components\homepage\searchBar\SearchBar.jsx 第 12:7 行:未使用状态字段:'inputValue' react/no-unused-state 第 17:21 行:未使用状态字段:'inputValue' react/no-unused-state
src\components\homepage\searchBar\input\AutoSuggestInput.jsx 第 46:5 行:必须使用解构道具分配 react/destructuring-assignment 第 46:16 行:道具验证中缺少“getValue”反应/道具类型
有人可以帮助我吗?我不明白为什么它没有被使用
解决方案
正如 React 所说,您的状态inputValue
已设置,但您没有在任何地方或任何操作中使用它。inputValue
您可以在使用状态时摆脱它。
比如放在组件的render()
方法中SearchBar
:
render() {
return (
<div className="searchBarItems">
<SearchInput getValue={(event, value) => this.getValue(event, value)} />
<SearchButtons />
<p>Current input value: {this.state.inputValue}</p>
</div>
);
}
第二个问题,它的 linting 错误,说你应该重组 props,你可以像下面这样摆脱它:
onChange = (event, { newValue }) => {
this.setState({
value: newValue,
});
const { getValue } = this.props;
getValue(event, newValue);
};
推荐阅读
- vim - 在 gVim 中插入特定字符串而不是制表符或空格
- php - 对子数组进行排序,以便每个子数组中的对应值按数字顺序排列?
- java - 如何使用单独的 Mysql 表使用 JPA 实现枚举
- python - 训练多标签分类模型时内存不足
- regex - Delphi正则表达式编写
- django - 如何在表单字段Django中更改日期格式
- r - 在 R DataFrame 中查找 Pattern 之后的匹配字符
- python - 使用 lmfit 进行高斯拟合
- reactjs - 如何找到错误认证护照并做出反应?
- amazon-web-services - 如何在 GameLift 中实现好友列表?