Trying to pass selected value from dropdown and substituting in path for redirection
React state updates are asynchronously processed, so you can't enqueue a state update and expect to use the updated result
state value within the same render cycle/callback scope. Just use the current label value from the onChange event to form the next target path
value. Add a leading "/"
so the link is treated as an absolute path, i.e. "/inventory/cluster/" + e.label
.
function Cluster() {
var clusters = [
{
value: 1,
label: "NA1"
},
{
value: 2,
label: "NA2"
},
{
value: 3,
label: "NA3"
},
{
value: 4,
label: "NA4"
}
];
const history = useHistory();
const [result, ddlvalue] = useState(clusters[0].label);
const routeChange = (e) => {
ddlvalue(e.label);
let path = "/inventory/cluster/" + e.label;
console.log(path);
history.push(path);
};
return (
<div>
<Select options={clusters} onChange={routeChange} />
<h1>{result}</h1>
</div>
);
}