I'm using useHistory and useLocation on two components. When passing data on history.push with a string "test" state will be "test". However, if I pass "test test" state becomes undefined. Also, useEffect runs twice which I cannot understand why. I have it pointing to search.
First Component
import React, { useState, useEffect } from "react";
import { useHistory } from "react-router-dom";
const history = useHistory();
const [value, setValue] = useState("");
const onKeyPress = event => {
if (event.key === "Enter" && event.target.value.length > 0) {
setValue(event.target.value);
event.preventDefault();
history.push({
pathname: "/home/Search",
search: `SearchText=${value}`,
state: { value }
})
}
}
Second Component Consuming Data
import React, { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";
const { search, state } = useLocation();
useEffect(() => {
console.log("search", search)
console.log("state", state)
}, [search])