簡體   English   中英

將 searchParam 數據從一個組件發送到 reactjs 中的另一個組件

[英]Send searchParam data from one Component to another component in reactjs

我有一個組件,它在下拉列表中顯示數據列表,並且有一個選項可以搜索這些用作過濾器的數據。 這是我的代碼:

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import Popover from '../../Popover';
import Input from '../../Input';
import Icon from '../../Icon';
import IconButton from '../../IconButton';

const DropDownFilter = props => {
  const { label, options, onChange, isSearchEnabled } = props;
  const [activeOption, setActiveOption] = useState({});
  const [filter, setfilter] = useState('');

  const searchFilter = event => {
    setfilter(event.target.value);
  };

  const removeFilter = () => {
    setfilter('');
  };

  const lowercasedFilter = filter.toLowerCase();
  const filteredData = options.filter(item => {
    return Object.keys(item).some(
      key => typeof item[key] === 'string' && item[key].toLowerCase().includes(lowercasedFilter)
    );
  });

  const labelText = activeOption.label ? activeOption.label : label;

  const handleSelectedOption = option => {
    setActiveOption(option);
    onChange(option);
  };

  return (
    <div className="filter">
      <Popover linkText={labelText} size="small" direction="bottom-left">
        {isSearchEnabled && (
          <div className="filter__search">
            <Input
              value={filter}
              onChange={searchFilter}
              preIcon={
                <div role="presentation">
                  <Icon name="search" />
                </div>
              }
              placeholder="Search"
              postIcon={
                filter.length > 0 && (
                  <IconButton
                    icon={<Icon name="close" />}
                    size="tiny"
                    onClick={removeFilter}
                    standalone={true}
                    isIconOnly={true}
                  />
                )
              }
            />
          </div>
        )}
        <ul className="filter__options filter__options--scrollbar">
          {filteredData.map(option => (
            <li
              key={option.value}
              role="presentation"
              className={classNames('filter__options-option', {
                'filter__options-option--active': option.value === activeOption.value,
              })}
              onClick={() => handleSelectedOption(option)}
            >
              {option.label}
            </li>
          ))}
        </ul>
      </Popover>
    </div>
  );
};

DropDownFilter.defaultProps = {
  label: 'Filter Menu',
  options: [],
  isSearchEnabled: true,
};

DropDownFilter.propTypes = {
  label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  options: PropTypes.arrayOf(
    PropTypes.shape({
      label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
      value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    })
  ),
  onChange: PropTypes.func.isRequired,
  isSearchEnabled: PropTypes.bool,
};

export default DropDownFilter;

這是它的 GIF: https://recordit.co/HtalUtuPsj

現在在搜索過程中,我想將搜索參數的值發送到另一個組件,該值將用於從數據庫或該新組件中正在處理的任何其他外部數據源進行搜索。 例如,如果我正在搜索Ratings ,該組件應該在它自己組件中的現有選項列表中搜索它,同時它會在任何其他外部數據源或數據庫中搜索Ratings 此外部網絡調用、搜索或任何其他功能將在其他組件中處理。 所以這個組件只會發送搜索參數; 例如實時對其他組件的Ratings

I can think of an idea like I will get the searchParam in a state and pass the setState value to a new props which will be called through an onSearchParamChange function, this new function will pass the data through a callback and the other component will get the通過調用該組件的該道具來獲取數據。 我不確定這是否是正確的方法,而且我也無法在代碼中實現這個想法。 有沒有更好的方法呢? 如果是這樣,那將是什么編碼實現?

如果您需要傳遞給父組件,您應該能夠使用例如傳遞給組件的onChange道具,就像您在handleSelectedOption function 中所做的那樣。 function 實際上是將選擇的選項傳遞給父組件。 如果您想在用戶鍵入時傳遞給父組件,那么您應該在 searchFilter 中調用onChange searchFilter

  const searchFilter = event => {
    const option = event.target.value);
    setfilter(option);
    onChange(option);
  };

如果要將其傳遞給子組件,則可以將其作為 prop 傳遞:

<ChildComponent filter={ filter } />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM