简体   繁体   中英

react-select how to display the option when click on using typescript

I'm still new to reactjs and typescript . I have a filter search bar that filters based on what the user inputs. This also display a drop-down list if clicked on.

The problem I have is when I click on the option: the bar doesn't display the option I click on. Even when I type some input then click on the options, a blank bar is shown.

What is the simplest way to display the option when it's clicked on?

Also, how can I move my data to a different file like JSON file and import it in here and use it in <Select/> in place of data.

import * as React from 'react'
import Select from 'react-select/'


const data = [
    {label: 'React', value: 'react'},
    {label: 'ReactNative', value: 'react-native'},
    {label: 'Java', value: 'java'},
    {label: 'CSS', value: 'css'},
]

function filter(props) {
    return <div>
        <Select options={data} placeholder="Enter some text to filter the list below" />
    </div>
}

export default filter;

What is the simplest way to display the option when it's click on?

I looked at your code and that looks like a functional component

They do not save an State and this is okay in this example. The component that is handling this filter component is the one that should have a state with the selected Option, and display it accordingly. I guess that "parent" component is not being shown here but is somewhere in your code.

Also, how can I move my data to a different file like json file and import it in here and use it in in place of data.

Create a.js file, define a const variable that holds the Json Data, and export it, kinda like this:

export const mockData = {... };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM