Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - What's the correct syntax for Data-Toggle Bootstrap on React.js

So I'm very new to react and javascript in general, I am just trying to lay it out how I see it in HTML

el.button({ 
    type: 'button',
    className: 'navbar-toggle',
    data-toggle: 'collapse', // Syntax Error Here
    data-target: 'navbar' // Syntax Error Here
}, 
el.span({
    className: 'sr-only'
}, 'Toggle Navigation'), 
el.span({
    className: 'icon-bar'
}),
el.span({
    className: 'icon-bar'
}), 
el.span({
    className: 'icon-bar'
})
),

Using purely JS React.JS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the React docs on Dom Differences which is a bit tucked away,

"All DOM properties and attributes (including event handlers) should be camelCased to be consistent with standard JavaScript style. We intentionally break with the spec here since the spec is inconsistent. However, data-* and aria-* attributes conform to the specs and should be lower-cased only."

That helps to determine the cases, which you already have correctly, but since hypens are not allowed for javascript variable names by default we also need to quote the identifiers.

So, using your example, the notation that works for me as of React 0.13.3 is:

el.button({ 
    type: 'button',
    className: 'navbar-toggle',
    "data-toggle": 'collapse', // Syntax Error Here
    "data-target": 'navbar' // Syntax Error Here
}, 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...