问题如下:
第一次进入到页面的时候,因为需要去fetch数据,所以会在没数据的情况下有一次渲染,等fetch成功后,会再一次进行渲染。
怎么解决第一次进入页面没数据的情况下,页面渲染时不报错
问题如下:
第一次进入到页面的时候,因为需要去fetch数据,所以会在没数据的情况下有一次渲染,等fetch成功后,会再一次进行渲染。
怎么解决第一次进入页面没数据的情况下,页面渲染时不报错
思路是条件渲染,设置一个判断条件,条件值不同,渲染的组件不同,下面是官网的一个例子
class LoginControl extends React.Component { constructor(props) { super(props); this.handleLoginClick = this.handleLoginClick.bind(this); this.handleLogoutClick = this.handleLogoutClick.bind(this); this.state = {isLoggedIn: false}; } handleLoginClick() { this.setState({isLoggedIn: true}); } handleLogoutClick() { this.setState({isLoggedIn: false}); } render() { const isLoggedIn = this.state.isLoggedIn; let button = null; if (isLoggedIn) { button = <LogoutButton onClick={this.handleLogoutClick} />; } else { button = <LoginButton onClick={this.handleLoginClick} />; } return ( <div> <Greeting isLoggedIn={isLoggedIn} /> {button} </div> ); } } ReactDOM.render( <LoginControl />, document.getElementById('root') );
没有的值默认给个空就可以了。例如<p>{data.code ? data.code : ''}</p> 然后在componentDidmount生命周期中去请求数据,然后在setState
fetch(url, method: 'Get')
.then(response => {
if(response){
message.success(请求成功
);
return JSON.stringify(response);
}
throw error(请求失败
);
})
.then(data => {
dispath(dataAction(data))
})
.catch(e => {
console.log(e)
})
可以使用fetch-initial-data插件
https://github.com/tzuser/fet...
npm install --save fetch-initial-data
https://github.com/tzuser/ssr_base
import {bindActionCreators} from 'redux';
class Home extends Component{
constructor(props){
super(props)
//判断是否是浏览器
if(typeof window==="object")this.fetchData();
}
//fetchData固定名称,必须返回异步,且所有action需要awiat
async fetchData(){
await this.props.getDataAct();
}
render(){
let {data}=this.props;
return <div>首页 {data} </div>
}
}
const mapStateToProps=(state)=>({
data:state.config.data
})
const mapDispatchToProps=(dispatch)=>bindActionCreators({
getDataAct:Acts.getData,
},dispatch)
export default connect(mapStateToProps,mapDispatchToProps)(Home)
import {getDataFromTree} from 'fetch-initial-data';
const render=async (ctx,next)=>{
//获取store
const { store, history } = createServerStore(ctx.req.url);
const AppRender=(
<Provider store={store}>
<ConnectedRouter history={history}>
<App/>
</ConnectedRouter>
</Provider>
)
//保证所有请求完成
await getDataFromTree(AppRender);
//取得state传入html
let state=store.getState();
//开始渲染
let routeMarkup =renderToString(AppRender)
}
//方法一:骨架屏或loading图
{market_topic && market_topic.data.topic_list ? market_topic.data.topic_list.map((item, index) => (
<a key={index} href={item.link}>
<li className="topic-item">
<div className="topic-photo">
<div className="_3kP-rt0_ _37xwNlCe">
<i className="_2AmRNPt0" style={{ 'marginTop': '100%' }}></i>
<div className="_1uN_mkij" style={{ 'backgroundImage': `url(${item.img})`, 'backgroundSize': 'cover', 'backgroundRepeat': 'no-repeat', 'backgroundPosition': 'center center' }}></div>
</div>
</div>
</li>
</a>
)) : <Skeleton active="true" />}
//方法二:
{market_topic && market_topic.data.topic_list.map((item, index) => (
<a key={index} href={item.link}>
<li className="topic-item">
<div className="topic-photo">
<div className="_3kP-rt0_ _37xwNlCe">
<i className="_2AmRNPt0" style={{ 'marginTop': '100%' }}></i>
<div className="_1uN_mkij" style={{ 'backgroundImage': `url(${item.img})`, 'backgroundSize': 'cover', 'backgroundRepeat': 'no-repeat', 'backgroundPosition': 'center center' }}></div>
</div>
</div>
</li>
</a>
)) }
====================
render() {
const { market_people } = this.props
return (
market_people &&
<section className="user-bar">
<a className="user-bar-cart" href={market_people.data.cart_url}>
购物车<em className="user-bar-cart-num">{market_people.data.cart_num ? market_people.data.cart_num : ''}</em>
</a>
<i className="user-bar-line"></i>
<a className="user-bar-people" href={market_people.data.people_url}> 我的豆品</a>
</section >
)
}
3 回答1.9k 阅读✓ 已解决
1 回答1.6k 阅读✓ 已解决
4 回答1.6k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
1 回答2.5k 阅读✓ 已解决
2 回答1.3k 阅读✓ 已解决
2 回答1.6k 阅读✓ 已解决
加个 loading 的状态.