I don’t know why but this does not sit right with me even though it works. Can someone double check my code?
Expected: User makes a selection in Child component. That selection should be sent up to Parent where it will be incorporated into other code.
Actual: As expected, no worries there.
My question is: am I coding this the right way?
Child component:
import React, { useState, Fragment } from "react";
const Autocomplete = (props) => {
//a bunch of code
function onKeyDown(e) {
if (e.keyCode === 13) {
//...
setUserInput(filteredOptions[activeOption]);
sendIngredients(filteredOptions[activeOption]);
//...
}
// a bunch of code
function sendIngredients(ingredient) {
props.parentCallback(ingredient);
}
//more code
return (
<Fragment>
<div className="search">
<input
type="text"
className="search-box"
onChange={onChange}
onKeyDown={onKeyDown}
/>
}
Parent component:
function IngredientPickerHooks(props) {
//....
function callbackSetIngredients(ingredientChild) {
console.log(ingredientChild); //something will be done with this
//...
}
return (
//...
<Autocomplete
parentCallback={(ingredientChild) =>
callbackSetIngredients(ingredientChild)
}
/>
//...
);
}
The part that seems wrong to me is that I have added ingredientChild
in parentCallback
. I feel like there is a cleaner way to do this.