Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  124] [ 4]  / answers: 1 / hits: 23256  / 8 Years ago, wed, august 3, 2016, 12:00:00

Basically I have 3 custom attributes data-pageName, data-defaultOption, data-options.



The problem I have is that when I pass into my child component I get an unexpected token error because its something like this:



const pageContent = ({data-pageName, name, onChange, data-defaultOption, value, data-options}) => {
/*return here*/
};


Basically the - symbol is causing the error.



How do I include it as data-pageName and not read as data - pageName?



This is how I call the component:



<pageContent data-pageName={this.state.pageClicked} onChange={this.closeMenu} data-defaultOption={this.state.tmpDefaultOption} value={this.state.tmpValue} data-error={this.state.tmpError} data-options='a'/>

More From » reactjs

 Answers
7

Dashes are not allowed in variable names. So, you have to use quotes ''



const Example = (props) =>
<div>{props['data-name']}</div>

ReactDOM.render(
<Example data-name=hello/>,
document.getElementById('app')
)

<script src=https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js></script>
<div id=app />




[#61163] Monday, August 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billosvaldor

Total Points: 601
Total Questions: 113
Total Answers: 113

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;