我正在尝试构建一个搜索框。但是,当我想在输入内容时设置状态时,该框只是不允许我输入任何内容。
这是调用搜索框组件的类
export default class Tagsearch extends Component {
constructor(props) {
super(props);
this.state = {
hitsDisplay:false,
inputContent:"",
tags:[]
};
}
openDisplay = () => {
console.log("open")
// this.setState({ hitsDisplay: true })
}
closeDisplay = () => {
console.log("close")
// this.setState({ hitsDisplay: false })
}
render() {
let inputTags = (
this.state.tags.map((tag, i) =>
<li key={i} style={styles.items}>
{tag}
<button onClick={() => this.handleRemoveItem(i)}>
(x)
</button>
</li>
)
)
let result = (
<div className="container-fluid" id="results">
</div>
)
if (this.state.hitsDisplay) {
result = (
<div className="container-fluid" id="results">
<div className="rows">
<MyHits handleSelect={this.handleSelect}/>
</div>
<div className="rows">
<Pagination />
</div>
</div>
)
}
return (
<InstantSearch
appId="*******"
apiKey="***********************"
indexName="tags"
>
{inputTags}
<SearchBox
connectSearchBox={connectSearchBox}
openDisplay={this.openDisplay}
closeDisplay={this.closeDisplay}
/>
{result}
</InstantSearch>
)
}
}
下面是搜索框组件
const SearchBox = props => {
let {
connectSearchBox,
openDisplay,
closeDisplay
} = props;
const CustomSearchBox = connectSearchBox(({ currentRefinement, refine }) => {
const handleChange = (e, refine) => {
refine(e.target.value)
// console.log(e.target.value)
if (e.target.value !== "") {
openDisplay();
} else {
closeDisplay();
}
}
如果我在 open & closeDisplay 中评论两个 setState,它工作正常,它会相应地打印出打开和关闭。但是,一旦我启用了 setState,输入框就不允许我输入任何内容。
红糖糍粑
相关分类