I am trying to build a search box. However, when I want to set the state when something is typed in, the box just simply doesn't let me type in anything.
This is the class which calls the component of search box
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>
)
}
}
The following is the search box component
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();
}
}
return (
<label>
<ul style={styles.container}>
<input
style={styles.input}
type="text"
value={currentRefinement}
onChange={e => handleChange(e, refine)}
/>
</ul>
</label>
)
})
return (
<CustomSearchBox />
)
}
export default SearchBox;
If I comment the two setStates in open & closeDisplay, It work fine, it prints out open and close accordingly. However, once I enable setState, the input box simply doesn't allow me to type in anything.
Any help is appreciated.