score:14
Accepted answer
I figured it out. See below for a summary:
The important pieces are to make sure that your props are being passed into your child as a single array, you should have a function when calling your function (e.g. onClick={() => triggerParentUpdate('edit')}
), and when passing your update function to the child from the parent, you need something like this <RecipeActions pageState = {pageState} triggerParentUpdate = {updatePageState} />
Parent
function Recipe(recipe) {
const [pageState, setPageState] = useState("view");
const updatePageState = (state) => {
setPageState(state);
}
return (
<div className="border-b-2 border-gray-300 py-2">
<div className="h-full flex-col md:flex md:flex-row md:justify-between md:items-start">
<div
className="order-2 flex flex-col flex-1 px-2 h-full md:h-72 lg:h-64 md:flex-col md:justify-between md:order-1 md:w-1/2 lg:w-3/4"
>
<RecipeActions pageState = {pageState} triggerParentUpdate = {updatePageState} />
</div>
</div>
</div>
)
}
Child
function RecipeActions({pageState, triggerParentUpdate}){
const [open, moreActions] = useState(false);
return(
<div className="flex">
<span className={`${pageState=='view' ? 'hidden' : ''} ml-1 sm:ml-2 md:ml-1 lg:ml-2 shadow-sm rounded-md`}>
<button
onClick={() => triggerParentUpdate('edit')}
type="button"
className="inline-flex items-center px-3 sm:px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-gray-700 bg-white hover:text-gray-500 focus:outline-none focus:shadow-outline-blue focus:border-blue-300 active:text-gray-800 active:bg-gray-50 active:text-gray-800 transition duration-150 ease-in-out"
>
<svg className="h-5 w-5 text-gray-500" fill="currentColor" viewBox="0 0 20 20">
<path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z" />
<path
fillRule="evenodd"
d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z"
clipRule="evenodd"
/>
</svg>
<p className="hidden sm:block sm:pl-2 md:hidden lg:block">Edit</p>
</button>
</span>
</div>
)
}
and here is another example I saw from this page
import React from 'react';
function App() {
const [fruits, setFruits] = React.useState([
{ id: '1', name: 'Apple', isFavorite: false },
{ id: '2', name: 'Peach', isFavorite: true },
{ id: '3', name: 'Strawberry', isFavorite: false },
]);
function handleClick(item) {
const newFruits = fruits.map((fruit) => {
if (fruit.id === item.id) {
return {
id: fruit.id,
name: fruit.name,
isFavorite: !fruit.isFavorite,
};
} else {
return fruit;
}
});
setFruits(newFruits);
}
return (
<div>
<h3>with no styling</h3>
<Basket items={fruits} onClick={handleClick} />
</div>
);
}
function Basket({ items, onClick }) {
return (
<ul>
{items.map((item) => (
<li key={item.id}>
{item.name}
<button type="button" onClick={() => onClick(item)}>
{item.isFavorite ? 'Unlike' : 'Like'}
</button>
</li>
))}
</ul>
);
}
export default App;
Source: stackoverflow.com
Related Query
- Updating Parent Component State from Child Component with UseState React Hook
- How to change a value from parent component in a child component with useState React hook
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- Problem updating parent state from child component in React
- Updating state from an array with react useState hook
- Update state on Parent component from input in Child using props and useState Hook
- How to properly pass an array from a parent React class component state to a child component written with hooks?
- React - Updating Parent State from Child Form Component
- React Child Component Not Updating After Parent State Change
- React js change child component's state from parent component
- React Hook : Send data from child to parent component
- Updating state with props on React child component
- Change react hook state from parent component
- How to use React Context with useState hook to share state from different components?
- React Child with useEffect() not updating on Parent State Change
- Sharing state from parent to child component in React
- Access child state of child from parent component in react
- How to communicate from Child Component to Parent Component with React Router
- Updating Child Component Via Change of Props from Parent in React
- How to update parent state from child component in React + send a paramater
- Watching state from child component React with Material UI
- react js set state from parent to child component
- React How to change state value from child to Parent using useState and useContext
- Can i set state in parent from child using useEffect hook in react
- Update parent component State from child component in react js
- Updating Parent Component state from multiple child components' componentDidMount() synchronously
- Setting and updating state in React JS with Dropdown component from Material UI
- Updating Parent component state from multiple child components not taking updated state value into account
- Updating state from child component to parent component
- React 16.8 hooks - child component won't re-render after updating parent state
More Query from same tag
- Search Function unresponsive in React
- How to have nested loops with map in JSX?
- material ui "sx" prop and computed values
- Consistent left justify on phone application
- How to call two functions onClick() react/redux
- Image file getting corrupted on uploading to amazon s3 public bucket using reactjs
- How to collect data from json file and then add those separetely in a component
- React/Typescript Property 'data' does not exist on type 'IntrinsicAttributes & [Interface]'
- In django's view.py, I want to change it to a string form rather than a json form
- Why's my response being called more than once when the variable's passed down from top level App component?
- Enzyme/Jest Context API Injection (React) Not Working
- Build the docker with react app throws error
- When mapping over an array to generate React components, array order is not respected
- how to remove duplicate value from option in react js?
- How to show validation message on <TagsInput> react premade component on unique value
- How to define an object whose items can be accessed through a variable content in Typescript?
- React router dom - Form onSubmit pass input value to another component as well as redirect to that component
- How to authenticate existing users in new microservices
- Value of variable gets reset on submit in react form
- Parsing error: Unexpected token, expected "," in react jsx when adding <img>
- Jest/Enzyme | Redux prop is not defined in test
- Warning: flattenChildren(...): Encountered two children
- Transitioning between the render of two components in React [React-Spring]
- Data from the backend is not appearing when you assign it into new object on the front end
- change state for onClick event in other component react
- Nextjs - how to apply styled-jsx to jsx returned from method
- Absolute import using Webpack resolve.alias in a React app
- React Child Component Not Rendering
- Using querySelectorAll on a React component's children
- React: Prevent Right Click from focusing an otherwise focusable element