React component state not updating with passed props

Tags: , , ,

I have created an Accordion component which has data(object) and expanded(boolean) as props. expanded props is used to set the expanded/collapsed state of this component passed as a prop.

const DeltaAccordion = ({ index, data, expanded = true }) => {

Accordion component also has an internal state

const [isExpanded, setIsExpanded] = useState(expanded);

which is used for expanding/collapsing the accordion. Below is my complete component


import React, { useState } from "react";

// styles
import styles from "./index.module.scss";

const Accordion = ({ index, data, expanded = true }) => {
  // state
  const [isExpanded, setIsExpanded] = useState(expanded);

  console.log(, `prop-val==${expanded}`, `inner-state==${isExpanded}`);
  return (
      className={`${styles.container} caption ${isExpanded && styles.expanded}`}
      <div className={styles.header} onClick={() => setIsExpanded(!isExpanded)}>
      <div className={styles.content}>
        {data.newValue && (
          <div className={styles.newValue}>
        {data.oldValue && (
          <div className={styles.oldValue}>
export default Accordion;

The parent component has a list of data and it loops through the list to create an accordion for each data item.

App.js file

{, index) => (
        <Accordion data={data} expanded={!collpaseAll} index={1} />

Here goes problem

There is also a button in my App.js file which is for either expanding/collapsing all the accordions together.
But when I pass its value as prop expanded to the accordion component then this value is not getting applied to the internal isExpanded state of the accordion component.

Here is live running code at codesandbox:


Inside the Accordion

const [isExpanded, setIsExpanded] = useState(expanded);

This line will take first time(on first render) value. To assign it next time(rerender) value you need to add a effect

useEffect(() => {
}, [expanded]);

And in your case, you can use the props expanded directly inside Accordion, you dont need to take it in local state.

Source: stackoverflow