I have a React Router Link that calls a submitInput function when I click on it.
<Link onClick={submitInput} to={queryString}>Submit</Link>
This is what the submitInput function looks like. What it does is that it gets value from an input element and updates a state variable named QueryString.
function submitInput() {
var inputTCN = $('#inputTCN').val();
var finalString = "?name=" + inputTCN;
setQueryString(finalString);
}
This is what the state variable looks like.
const [queryString, setQueryString] = React.useState("?name=");
This is my problem. Let us say someone writes "hello" in the input element and clicks on Submit Link. The url changes to "?name=" and not "?name=hello". Let us say the input element is now changed to "hello2". The url changes to "?name=hello" and not "?name=hello2". And if the input element is changed to "hello3", the url changes to "?name=hello2" and not "?name=hello3".
The url parameter is always one step behind the state of QueryString. How do i prevent this?
PS: If I click the Submit Link twice, the url parameter and QueryString state will match.