1

I have an array of objects that pass as props to child component. in child component I want to map that array to show in a list. but I got this error

Invalid attempt to destructure non-iterable instance

this is my child :

const ServiceTabs = (props) => {
  const [parentProps] = props;
  return (
    <ul>
      {
          parentProps.map((content) => (
            <li key={content.id} className="active">
              <BtnButton
                btnStyle={content.btnStyle}
                btnColor={content.btnColor}
                customTitle={content.customTitle}
                IconSRC={content.IconSRC}
              />
            </li>
          ))
}
    </ul>
  );
};

and this is my parent:

 const ServiceCategory = () => {
      const [serviceState, setserviceState] = useState([
        {
          id: 1,
          customtitle: 'hair', 
          btnStyle: {
            backgroundColor: '#fff',
            width: '100px',
            height: '100px',
            flexDirection: 'column',

          },
        },
        {
          id: 2,
          .
          .
          },        
      ]);
.
.
.
 <ServiceTabs list={serviceState} />

when i change const [parentProps] = props; to const parentProps = props; the error changes:

parentProps.map is not a function i think my problem is destructure of props.but i dont know how to do that. hope my question was clear.

2
  • Your prop is named list, so you'd access that instead: props.list.map(... Commented Nov 24, 2019 at 20:21
  • oh oh!.thank you men.it worked@Clarity Commented Nov 24, 2019 at 20:26

4 Answers 4

1

You should just be destructuring the list prop like this and then map over that variable:

const { list } = props
list.map(content => {...})

Here is a working Codesandbox example using (mostly) your code to illustrate what I mean: https://codesandbox.io/s/distracted-pasteur-k8387?fontsize=14&hidenavigation=1&theme=dark

Sign up to request clarification or add additional context in comments.

5 Comments

when i use this way it throw an error >TypeError: Cannot read property 'map' of undefined
See the codesandbox link above for a working example
yes, that's working properly.and can you tell me how to fix eslint >'list.map' is missing in props validation eslint(react/prop-types) error? `i mean to show how to add propTypes and defaultProps
I'd appreciate an upvote if this was helpful, for proptypes just add a propType of list: arrayOf(Object) and make sure to import { arrayOf, Object } from 'prop-types' set the defaultProps list value to []
was this the accepted answer? If so could you click on the accepted answer tick to help others who might visit this question?
1

as @Clarity pointed out in comment I solved the problem by changing

const [parentProps] = props;

to

const parentProps = props;

and

parentProps.map((content) => (

to

parentProps.list.map((content) => (

Comments

0

Change const [parentProps] = props; to const {list: parentProps} = props;

Note that the [] in this context is used to destructure arrays, and that is why it shows the error:

Invalid attempt to destructure non-iterable instance

when you are destructuring an object you should pass in the destructor what is the field you want to take from the source object. More info can be found here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

2 Comments

thank you, it worked in this way but eslint give error>' list' is missing in props validation. and i dont want add props validation.
You are using propTypes?
-1

you should use

props.parentProps.map(content => {...})

4 Comments

I don't know because of hook or not but when changing my code this way that throw error.>Cannot read property 'map' of undefined
above you seem to destructure prop not correctly, it should be: const {parentProps} = props; instead of; const [parentProps} = props;
this is incorrect, there's no parentProps prop passed to child component.
@Berk Elmas you got it wrong. my props are an array of objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.