I have a dropdown in my application, and I want it so that when a user selects a different option from the dropdown, it should update the url.
For example, if I have a DropDown
component like such:
class DropDown extends React.Component {
render() {
return (
<select>
<DropDownOption name="foo" />
<DropDownOption name="bar" />
</select>
);
}
}
And a DropDownOption
component like such:
import { Link } from 'react-router-dom';
class DropDownOption extends React.Component {
render() {
return (
<option>
<Link to={`/value=${this.props.name}`}>
</Link>
</option>
);
}
}
If the user clicks on the dropdown then selects 'foo', the url would change to for example: "localhost:3000/value=foo
",
and when they select 'bar', it would change to "localhost:3000/value=bar
"
However, this doesn't work because <option>
tag cannot have an <a>
tag inside of it, but I was hoping to do it using <Link>
from the react-router-dom
module because I have been using it in other places (eg. for buttons) to change the route parameters and it has worked fine.
Any suggestions how I can implement this?
To set selected value of a select drop down to null with React, we can set the state we use as the value of the value prop of the select drop down to null . to set onChange to a function that calls setSelected to e. target. value which is the selected value if it's truthy.
Firstly wrap all the content of your page inside the return function inside the . Next, create the induvial routes inside the component. For each route, we have the path and the element props, these tell the path on the address bar and the component to be rendered out respectively.
Go to Formulas > Name Manager. In the Name Manager box, click the named range you want to update. Click in the Refers to box, and then on your worksheet select all of the cells that contain the entries for your drop-down list. Click Close, and then click Yes to save your changes.
Attach an onChange
event to the select and in it push to the history
object that the router passes as a prop.
Edit:
here's an example https://codesandbox.io/s/w031p82nr5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With