本质上讲,本章仍是说 JSX
,做为一个组件关键是渲染,因此我做为一个重点,单独写一章,方便你们理解上有个重点。html
理论上来讲,渲染分两种方式 JSX
React.createElement
(我在本文最后会稍微点到下) ,如何灵活使用你们本身权衡,存在必有理由。前端
掌握常见渲染控制react
组件嵌套并行,这是很是常见的状况,特别是容器组件,后面会细说git
代码github
function UserView(props) { return <p>用户 xxx 你好,欢迎回来!</p> } function GuestView(props) { return <p>游客你好, 注册 | 登录。</p> } function RenderView() { return ( <div> <UserView /> <GuestView /> </div> ) }
输出redux
codepensegmentfault
https://codepen.io/ducafecat/...react-router
代码dom
// 显示组件 function UserView(props) { return <p>用户 xxx 你好,欢迎回来!</p> } function GuestView(props) { return <p>游客你好, 注册 | 登录。</p> } // 条件判断 function ConditionView(props) { return <div>{props.isLogin && <UserView />}</div> } // 容器 function RenderView() { return ( <div> <ConditionView isLogin /> </div> ) }
条件控制中间用
&&
符号连接
你们能够动手改下看看函数
<ConditionView isLogin={false} />
输出
codepen
https://codepen.io/ducafecat/...
代码
// 显示组件 function UserView(props) { return <p>用户 xxx 你好,欢迎回来!</p> } function GuestView(props) { return <p>游客你好, 注册 | 登录。</p> } // 三目运算 function InlineIfElseView(props) { return <div>{props.isLogin === true ? <UserView /> : <GuestView />}</div> } // 容器 function RenderView() { return ( <div> <InlineIfElseView isLogin={false} /> </div> ) }
三目运算
在条件控制里, 仍是很实用的
输出
codepen
https://codepen.io/ducafecat/...
代码
// 数据 let blogs = [ { title: 'React v16.3.0: New lifecycles and context API', url: 'https://reactjs.org/blog/2018/03/29/react-v-16-3.html', by: 'Brian Vaughn' }, ... ] // 循环 function ListsView(props) { let datas = props.data return ( <ol> {datas.map((item, index) => ( <li key={index.toString()}> <a href={item.url} target="_blank"> {item.title} </a>{' '} --- <small>by {item.by}</small> </li> ))} </ol> ) } // 容器 function RenderView() { return ( <div> <ListsView data={blogs} /> </div> ) }
表达式用的{ ... }
符号包裹起来
表达式内部的JSX
用( ... )
包裹
表达式内部的JSX
也要注意必须有顶级标签
对于循环体内 必须有key
属性,map
内部惟一就行
输出
codepen
https://codepen.io/ducafecat/...
代码
// 数据 let blogs = [ { title: 'React v16.3.0: New lifecycles and context API', url: 'https://reactjs.org/blog/2018/03/29/react-v-16-3.html', by: 'Brian Vaughn' }, ... ] let types = ['推荐', '热门', '最新'] // 嵌套循环 function NestListsView(props) { let datas = props.data let types = props.types return ( <ul> {types.map((item, index) => ( <li key={index.toString()}> {item} <ol> {datas.map((item, index) => ( <li key={index.toString()}> <a href={item.url} target="_blank"> {item.title} </a>{' '} --- <small>by {item.by}</small> </li> ))} </ol> </li> ))} </ul> ) } // 容器 function RenderView() { return ( <div> <NestListsView types={types} data={blogs} /> </div> ) }
第二层在JSX
中的循环须要用{ ... }
包裹
这里的key
只要同层惟一就行,不用全局惟一
输出
codepen
https://codepen.io/ducafecat/...
虽然上面代码能运行,一旦代码复杂了,是不适合阅读和维护的,咱们须要把 datas
的循环封装成组件,代码以下
代码
function ListsView(props) { let datas = props.data return ( <ol> {datas.map((item, index) => ( <li key={index.toString()}> {item.title} </li> ))} </ol> ) } function NestListsView(props) { let datas = props.data let types = props.types return ( <ul> {types.map((item, index) => ( <li key={index.toString()}> {item} <ol> <ListsView data={datas}> </ol> </li> ))} </ul> ) }
NestListsView
中嵌套ListsView
,这样看起来舒服多了吧
代码
// 空组件 function NullView(props) { let isNull = props.isNull if (isNull) { return null } return <div>组件123</div> } // 容器 function RenderView() { return ( <div> <NullView isNull /> </div> ) }
这个空组件的做用 就是用来阻止显示,好比这里
isNull = true
的状况下,就没有显示的必要了
输出
codepen
https://codepen.io/ducafecat/...
代码
// ref引用 class RefInputView extends Component { constructor(props) { super(props) this.inputRef = React.createRef() } render() { return <input type="text" ref={this.inputRef} /> } componentDidMount() { this.inputRef.current.focus() this.inputRef.current.value = '123' } } // 容器 function RenderView() { return ( <div> <RefInputView /> </div> ) }
ref
提供了一种操做组件内部方法的接口,didMount
的时候获取焦点和设置值
输出
codepen
https://codepen.io/ducafecat/...
React.createElement
方式咱们先看下函数定义
createElement函数有三个参数
参数 | 说明 |
---|---|
type | html 标签名称, 默认 input |
props | 组件属性 |
children | 组件内容、子节点 |
代码
// 代码 function CreateElement(props) { let toWhat = props.toWhat let ele = React.createElement('div', null, `Hello ${toWhat}`) return ele } // 容器 function RenderView() { return ( <div> <CreateElement toWhat="你好" /> </div> ) }
输出
codepen
https://codepen.io/ducafecat/...