Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing route parameters when a different dropdown option is selected in React

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?

like image 483
K. Smith Avatar asked Jul 11 '18 01:07

K. Smith


People also ask

How do you change the selected value of dropdown in React?

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.

How do you conditionally route in React?

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.

How do I change dropdown list from one dropdown to another?

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.


1 Answers

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

like image 63
aaronjkrause Avatar answered Oct 09 '22 20:10

aaronjkrause