【js】基于React的Footer实现

Footer的基本原理

<html style="height:100%">
  <head>
  </head>
  <body style="min-height: 100%;margin-top: 0;margin-bottom: 0; display: flex;flex-direction: column">
    <div id="app" style="flex-grow: 1">
        	<p>Content</p>
   </div>
	<div style="display: flex;flex-direction: column;align-items: center;justify-content:flex-end;flex-grow: 1">
		<footer id="footer">
			<p style="">Designed by Fulton Shaw</p>
		</footer>
	</div>
  </body>
</html>

实现效果:
在这里插入图片描述
几个关键点:

  • <html style="height:100%"> 让HTML布满整个可视界面
  • body采用flex布局
  • body的两个子元素的flex-grow: 1,让他们平分空间
  • footerjustify-content: flex-end会尽量让元素下沉

基于骨架和replace(有问题)

当使用React时,我们面临的问题是如何使得布局参数生效。React渲染之后,元素并不是替代目标节点,而是作为目标节点的子元素,所以在React组件上的布局不生效。
一种可行的办法时,index.html作为骨架,然后index.js中手动渲染并替换元素:

<html style="height:100%">
  <head>
  </head>
  <body style="min-height: 100%;margin-top: 0;margin-bottom: 0; display: flex;flex-direction: column">
	  <!-- to be replaced by the script-->
	  <div id="app"></div>
	  <div id="footer"></div>
  </body>
</html>

Footer.js

function Footer(){
	return (
    <div style={{display: "flex",flexDirection: "column",alignItems: "center",justifyContent:"flex-end",flexGrow: 1}}>
		<footer id="footer">
			<p>Designed by Fulton Shaw</p>
		</footer>
    </div>
	)
}

export default Footer

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Footer from './Footer'

let app = document.createElement("div")
ReactDOM.render(
	  <React.StrictMode>
		    <App />
	  </React.StrictMode>
	,
	app
)
// <div id="app" style="flex-grow: 1">
app.children[0].style["flex-grow"] = 1

let footer = document.createElement("div")
ReactDOM.render(
	<React.StrictMode>
	    <Footer/>
	</React.StrictMode>
	,
	footer
)

// replace root with the rendered
document.body.replaceChild(app.children[0],document.getElementById('app'))
document.body.replaceChild(footer.children[0],document.getElementById('footer'))

上面的代码中, 我们通过ReactDOM.render将组件渲染到一个临时元素,然后获取临时元素的子组件来替换index.html中的骨架节点。

虽然这种方式在视觉上能够工作,但其问题在于,替换之后的元素,onClick等回调函数不生效。我猜这与创建元素时的上下文有关。

基于骨架和设置

因为替换会存在问题,所以我们不会更改React渲染的元素。我们基于上面的布局原理,还是构造骨架包含两个基本插入节点:

<html style="height:100%">
  <head>
  </head>
  <body style="min-height: 100%;margin-top: 0;margin-bottom: 0; display: flex;flex-direction: column">
	  <div id="app" style="flex-grow: 1"></div>
	  <div id="footer" style="display: flex;flex-direction: column;align-items: center;justify-content:flex-end;flex-grow: 1"></div>
  </body>
</html>

然后Footer.js现在只需要包含一个footer元素即可:

function Footer(){
	return (
		<footer id="footer">
			<p>Designed by Fulton Shaw</p>
		</footer>
	)
}

export default Footer

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Footer from './Footer'

ReactDOM.render(
	  (<React.StrictMode>
		    <App />
	  </React.StrictMode>
	  )
	,
	document.getElementById("app")
)
ReactDOM.render(
	(<React.StrictMode>
	    <Footer/>
	</React.StrictMode>
	),
	document.getElementById("footer")
)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值