onclick funtion not working beacause it not in the main retun 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

 const history = useHistory();
 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>
    </>
  );
};


Answer

From what I can see you’ve simply neglected to provide a routing context to your application. When uncommenting the history.push(`/detiled/${e}`); line in Searchclients and attempting some search/navigation I see the following error:

TypeError
Cannot read properties of undefined (reading 'push')

Assuming this is the same error you have then the solution is simple: wrap the app in a Router component in order to provide a routing context.

Example:

App.js

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

index.js:

import { BrowserRouter as Router } from "react-router-dom";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <StrictMode>
    <Router>
      <App />
    </Router>
  </StrictMode>,
  rootElement
);

Edit onclick-funtion-not-working-beacause-it-not-in-the-main-retun-statement