In my text box, when I type 123 and edit as 1-23, I need to trigger a separate api call and for 123 I need to do separate api call, but right now its capturing the onChange api call. when ever I enter dash between numbers I should call another api, but its calling the same api in on change
I found this links but nothing helping me How to get old Value with onchange() event in text box
Can you tell me how to fix it?
I'm providing my code snippet and sandbox below. All my code is present inside Button.js
which is inside the containers folder:
https://codesandbox.io/s/sad-ardinghelli-zvco8
function useHackerNews(props) {
const channel = useSelector(state => state.channel);
const dispatch = useDispatch();
const getPosts = channel => dispatch(fetchPosts(channel));
const getAlert = () => dispatch(displayAlert());
document.onmouseup = () => {
console.log(window.getSelection().toString());
};
const onKeyDown = e => {
console.log("e.keyCode", e.keyCode);
if (e.keyCode === 8) {
console.log("delete---->", e.target.value);
getPosts(channel);
if (e.target.value === "") {
console.log("onKeyDown delete empty value--->", e.target.value);
fetchSearch(channel);
}
}
};
const onMouseUp = e => {
console.log("onMouseUp e.keyCode", e.keyCode);
if (window.getSelection().toString().length > 0) return;
if (e.target.value === "") {
console.log("onMouseUp delete empty value--->", e.target.value);
fetchSearch(channel);
}
};
return (
<div>
<InputBase
// className={classes.input}
placeholder="Search Google Maps"
inputProps={{ "aria-label": "Search Google Maps" }}
onChange={e => {
if (e.target.value == "") {
return;
}
console.log("onChange e.target.value--->", e.target.value);
getPosts(channel);
}}
onKeyDown={() => {
// getPosts(channel);
onKeyDown();
}}
onMouseUp={() => {
// getPosts(channel);
onMouseUp();
}}
//onKeyDown={onKeyDown}
// onMouseUp={this.onMouseUp}
/>
</div>
);
}