user17181128
user17181128

Reputation:

on click function not working because its in another return statement

I am trying to make history by pushing on button click

onclick function of the li is not working

as u can see in the code <SuggestionsList/> is in the last return statement its funtions are rendered in const SuggestionsList = (props) => { . The onclick funtion is comimg inside the const SuggestionsList funtion, this is making the onclick funtion not working

i created the exact working in codesand and its working there without any problem i dont get why its not working in my local enter link description here

 function finddoctor(e) {
    console.log(e);
    history.push(`/detiled/${e} `);
  }

 
  const onChange = (event) => {
    const value = event.target.value;
    setInputValue(value);
    setShowResults(false);

    

    const filteredSuggestions = suggestions.filter(
      (suggestion) =>
        suggestion.firstname
          .toString()
          .toLowerCase()
          .includes(value.toLowerCase()) ||
        suggestion.id.toString().toLowerCase().includes(value.toLowerCase())
    );

  

    setFilteredSuggestions(filteredSuggestions);
    setDisplaySuggestions(true);
  };

  const onSelectSuggestion = (index) => {
    setSelectedSuggestion(index);
    setInputValue(filteredSuggestions[index]);
    setFilteredSuggestions([]);
    setDisplaySuggestions(false);
  };

  const SuggestionsList = (props) => {
  const {
      suggestions,
      inputValue,

      onSelectSuggestion,
      displaySuggestions,
      selectedSuggestion,
    } = props;

    if (inputValue && displaySuggestions) {
      if (suggestions.length > 0) {
        return (
          <ul className="suggestions-list" style={styles.ulstyle}>
            {suggestions.map((suggestion, index) => {
    
              const isSelected = selectedSuggestion === index;
              const classname = `suggestion ${isSelected ? "selected" : ""}`;
              return (
                <>
              
                  <li
                    style={styles.listyle}
                    onClick={finddoctor(suggestion.id)}
                    key={index}
                    className={classname}
                  >
                    {suggestion.firstname}
                  </li>
                </>
              );
            })}
          </ul>
        );
      } else {
        return <div>No suggestions available...</div>;
      }
    }
    return <></>;
  };


  useEffect(() => {
    axios
      .get("admin-panel/all-doctors-list/")
      .then((res) => {
        const data = res.data;
      setShowSerch(data);

      });
  
  }, []);

  return (
    <>
      <div className="note-container" style={styles.card}>
        <div style={styles.inner}>
          <p style={{ textAlign: "left" }}>Search Doctors</p>
          <form className="search-form" style={{}}>
            {showResults ? (
              <FontAwesomeIcon
                style={{ marginRight: "-23px" }}
                icon={faSearch}
              />
            ) : null}
            <input
              onChange={onChange}
              value={inputValue}
              style={styles.input}
              type="Search"
            />

            <SuggestionsList
              inputValue={inputValue}
              selectedSuggestion={selectedSuggestion}
              onSelectSuggestion={onSelectSuggestion}
              displaySuggestions={displaySuggestions}
              suggestions={filteredSuggestions}
            />
          </form>
         
        </div>
      </div>
    </>
  );
};


Upvotes: 0

Views: 109

Answers (1)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Instead of onClick={finddoctor(suggestion.id)} (Here just function invocation is happening and expected to have the callback method)

should be

onClick={() => finddoctor(suggestion.id)}

Upvotes: 1

Related Questions