Search Docs
在jsx中可以定义一个变量,然后赋值,然后交给虚拟DOM,用 {变量} 将变量包起来
jsx
DOM
{
}
const myId = 'YYY'; const VDOM = ( <h1 id={myId}> <p> 这是一个层层title </p> </h1> );
但是class标签类名就有点问题了
class
const myId = 'YYY'; const VDOM = ( <h1> <p class="title"> 这是一个层层title </p> </h1> );
上述代码中的class会产生warning
warning
处理方法就是将class改成className
className
const myId = 'YYY'; const VDOM = ( <h1 id={myId}> <p className="title"> 这是一个层层title </p> </h1> );
搞定,目的就是为了**避免ES6的class**声明类的用法
ES6
<div style={{backgroundColor:'black'}}></div>
注意点
标签必须闭合
<input type="text" /> //结尾加一个反斜杠将其闭合
html
使用js语法遍历数组,在jsx中的使用
js
const arr = ['JavaScript', 'CSS', 'HTML', 'React', 'Regular', 'Vue']; const VDOM = ( <div> <h1>前端学习</h1> <ul> { arr.map((item, index) => <li key={index}>{item}</li>) } </ul> </div> ) ReactDOM.render(VDOM, document.getElementById("#root"));