【发布时间】:2018-09-01 12:55:46
【问题描述】:
我使用 Redux 创建了一个带有 React 的组件,它在状态映射到 props 之前需要两次渲染。
使用此代码,
'user.private' 在第一次渲染时为空,在第二次渲染时为假
因此,在显示隐藏内容之前,加载页面会在显示“登录”之间闪烁一秒钟
我想默认显示登录文本,但如果用户的私有字段设置为 false,我实际上不希望它显示。
class Content extends React.Component {
render() {
const { user } = this.props;
let show = false;
if (user.private === false) show = true
return (
<section>
{
show
? <p>hidden content</p>
: <p>login</p>
}
</section>
)
}
}
const mapStateToProps = state => ({
user: state.store.user
});
export default connect(mapStateToProps, {})(Content)
【问题讨论】:
-
if (!user.private) show = true? -
产生同样的效果
标签: reactjs react-redux