React - render certain div based on react-select selectedOption

React - render certain div based on react-select selectedOption

I'm using&nbsp;<a href="https://github.com/JedWatson/react-select" target="_blank">react-select</a>&nbsp;to render two types of "job" options. From the dropdown the user is able to select either of the options. Based on the option selected they should render different divs that contain more input values. The input values are dependent on the&nbsp;selectedOption. I have two class components with render methods that should display the different inputs corresponding to the&nbsp;jobType

I'm using react-select to render two types of "job" options. From the dropdown the user is able to select either of the options. Based on the option selected they should render different divs that contain more input values. The input values are dependent on the selectedOption. I have two class components with render methods that should display the different inputs corresponding to the jobType

class App extends React.Component {
  constructor(props) {
    super(props);

this.state = {
  selectedOption: null
};

this.onChange = this.onChange.bind(this);
this.handleChange = this.handleChange.bind(this);

}

onChange = e => { this.set({ [e.target.name]: e.target.value }); console.log([e.target.value]); };

handleChange = selectedOption => { this.setState({ selectedOption }); console.log("Option selected: ", selectedOption); };

render() { const { selectedOption } = this.state; return ( <div> <fieldset> <legend>Parameters</legend> <label htmlFor="jobType" style={{ display: "block" }}> jobType: </label> <div> <Select value={selectedOption} onChange={this.handleChange} options={options} placeholder="Select a jobType..." isSearchable={options} /> </div> </fieldset> <div> {selectedOption === "Batch" ? ( <BatchParams /> ) : selectedOption === "Streaming" ? ( <StreamingParams /> ) : null} </div> </div> ); } }

So let's say when the Streaming option is selected from the dropdown the following class component should be rendered:

class StreamingParams extends React.Component {
      constructor(props) {
        super(props);

    this.state = {
      maxHits: 1
    };
    this.handleMaxHitsChange = this.handleMaxHitsChange.bind(this);
  }

  handleMaxHitsChange = e =&gt; {
    this.set({ [e.target.name]: e.target.valu });
  };
  render() {
    return (
      &lt;div&gt;
        &lt;label&gt;maxHits:&lt;/label&gt;
        &lt;input
          type="number"
          name="maxHits"
          onChange={this.handleMaxHitsChange}
          placeholder="1"
          min="1"
          step="1"
          required
        /&gt;
      &lt;/div&gt;
    );
  }
}

I can't quite get the additional input boxes to render, not sure if I'm taking the correct approach here. I have included a codesandbox with my issue.

javascript reactjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Mapbox GL Javascript wrapper for ReactJS

react-mapbox-wrapper Mapbox GL Javascript wrapper for ReactJS

JavaScript Expressions in JSX in ReactJs

Welcome, what is JSX Expressions in React Js in Hindi? JSX is an Expression Too After compilation, JSX expressions become regular JavaScript function calls a...

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.