简体   繁体   English

react-select 如何在点击 using typescript 时显示选项

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

I'm still new to reactjs and typescript .我对reactjstypescript还是新手。 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.另外,如何将我的数据移动到其他文件,如 JSON 文件并将其导入此处并在<Select/>中使用它来代替数据。

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.他们不保存 State,这在本例中是可以的。 The component that is handling this filter component is the one that should have a state with the selected Option, and display it accordingly.处理此过滤器组件的组件应具有 state 和所选选项,并相应地显示它。 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.另外,如何将我的数据移动到不同的文件,如 json 文件并将其导入此处并使用它代替数据。

Create a.js file, define a const variable that holds the Json Data, and export it, kinda like this:创建一个 .js 文件,定义一个包含 Json 数据的 const 变量,然后导出它,有点像这样:

export const mockData = {... };导出 const mockData = {... };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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