0

Im trying to save some data into a state using object keys in react,

Ex : setText(data[listname][0].feature-type;

But after saving the file it automatically adding a space between featute and type

Ex: setText(data[listname][0].feature - type:

As it is this will resulted as reference error.How to avoid this

1
  • 1
    Is feature-type a property? You need to access as a dictionary setText(data[listname][0]['feature-type']. Because there are some limitations to which names can be accessed with a dot (.). And since the property name contains hífen (-), it needs to be accessed as a dictionary. VSCode is formatting different then what you expect because you are giving it invalid syntax. Commented Apr 19, 2021 at 17:50

1 Answer 1

1

As commented, this is the solution if feature-type is a property of the object you are trying to access:

You need to access it as a dictionary setText(data[listname][0]['feature-type'].

Because there are some limitations to which names can be accessed with a dot (.). And since the property name contains hífen (-), it needs to be accessed as a dictionary. VSCode is formatting different then what you expect because you are giving it invalid syntax.

You can look at this answer: https://stackoverflow.com/a/29888297/8633918 for more details.

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

Comments

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.