我正在使用像这样的反应
首先,我使用mapStateToProps加载商店。在该功能中,im实际上能够读取商店。在那里读取的对象与this.state.renderedEl
基本相同,但是具有不同的值。
我的目标是删除该状态,并改用存储值。
这些值应在render()中读取,其中Im实际上使用this.state.renderedEl
值来评估某些条件。但是我不理解如何读取存储值
import React, { Component, useState } from "react";
import brand from "../images/valhallaNaranja.png";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faArrowAltCircleLeft, faArrowAltCircleRight, faUser } from '@fortawesome/free-regular-svg-icons'
import { faColumns } from '@fortawesome/free-solid-svg-icons'
import './css/Sidebar.css'
import { isAdmin } from "../services/userCheck.js"
import { connect } from "react-redux";
const mapStateToProps = state => {
//I CAN READ THE STORE THERE
console.log("-sidebar-")
console.log(state.renderedEl)
console.log("-sidebar-")
return { renderedEl: state.renderedEl }
}
class SideBar extends Component {
constructor(props) {
super(props);
this.state = {
retracted: this.props.retracted,
isAdmin: false,
isHovering: false,
//I WANT TO STOP USING THIS SO I CAN USE THE STORE DATA
renderedEl: {
heimdall: false,
skadi: false,
mercator: false
}
}
this.hoverTrue = this.hoverTrue.bind(this);
this.hoverFalse = this.hoverFalse.bind(this);
}
componentDidMount() {
if (isAdmin()) {
this.setState({
isAdmin: true
})
}
}
componentDidUpdate() {
if (this.state.retracted != this.props.retracted) {
this.setState({
retracted: this.props.retracted
})
}
}
renderEl = (el) => {
var elementName = el.target.getAttribute('id');
var renderedElements = this.state.renderedEl;
for (let key in renderedElements) {
if (key == elementName) {
renderedElements[key] = true
}
}
this.setState({
renderEl: renderedElements
})
}
hoverTrue() {
this.setState({
isHovering: true
})
}
hoverFalse() {
this.setState({
isHovering: false
})
}
render() {
let navbar_brand = this.state.retracted ? "navbar-brand-retracted" : "navbar-brand";
let img_redie = this.state.retracted ? "img-redie-retracted" : "img-redie";
let home_icon = this.state.retracted ? "divicon homeicon-retracted" : "divicon homeicon";
let register_icon = this.state.retracted ? "divicon divicon2 registericon-retracted" : "divicon divicon2 registericon";
let expand_icon = this.state.retracted ? "divicon-no-hover divicon2 expandicon-retracted" : "divicon divicon2 expandicon";
//I WANT TO READ THE STORE THERE, INSTEAD OF THE STATE VALUES
let skadiRendered = this.state.renderedEl.skadi ? "bubbletext bubbletext-rendered" : "bubbletext";
let heimdallRendered = this.state.renderedEl.heimdall ? "bubbletext bubbletext-rendered" : "bubbletext";
let mercatorRendered = this.state.renderedEl.mercator ? "bubbletext bubbletext-rendered" : "bubbletext";
let layoutAppVisualSelector = this.props.history.location.pathname == "/layout" ? "divicon divicon2 expandicon divicon-layout" : "divicon divicon2 expandicon";
return (
<div id="sidebar" className={this.state.retracted ? 'sidebar-retracted' : 'sidebar-expanded'}>
<div /*routerLink=""*/ className={navbar_brand}>
<img alt="Reddie" src={brand} width="60" height="60" className={img_redie} />
</div>
<ul className="nav nav3 navbar-nav">
<li>
{/* Home icon */}
<div className={home_icon} onClick={() => this.props.history.push('/')}>
<svg className="svgicon-sidebar" viewBox="0 0 14 14" >
<path d="M13.9 5.7L7.2.8c-.1-.1-.3-.1-.4 0L.1 5.7c-.1.1-.1.3 0 .5s.3.2.5.1L7 1.6l6.4 4.7c.1 0 .1.1.2.1s.2-.1.3-.1c.1-.3.1-.5 0-.6" />
<path d="M12.1 6.4c-.2 0-.4.2-.4.4v5.8H8.8V9.4c0-1-.8-1.8-1.8-1.8s-1.8.8-1.8 1.8v3.2H2.3V6.7c0-.2-.2-.4-.4-.4s-.4.2-.4.4v6.2c0 .2.2.4.4.4h3.6c.2 0 .3-.1.4-.3V9.4c0-.6.5-1.1 1.1-1.1.6 0 1.1.5 1.1 1.1v3.5c0 .2.2.3.4.3h3.6c.2 0 .4-.2.4-.4V6.7c0-.2-.2-.3-.4-.3" />
</svg>
</div>
</li>
{this.state.isAdmin ? <li>
<div className={register_icon} onClick={() => this.props.history.push('/admin')}>
<FontAwesomeIcon className="registerIcon" icon={faUser} />
</div>
</li> : null}
{(this.props.history.location.pathname != "/layout") && (this.props.history.location.pathname != "/skadi") && (this.props.history.location.pathname != "/heimdall") && (this.props.history.location.pathname != "/mercator") ? null : <li>
<div className={layoutAppVisualSelector} onMouseEnter={this.hoverTrue}
onMouseLeave={this.hoverFalse} >
<FontAwesomeIcon className="registerIcon" icon={faColumns} onClick={() => this.props.history.push({ pathname: '/layout', state: { comeFrom: this.props.history.location.pathname } })} />
{(this.state.isHovering && this.props.history.location.pathname == "/layout") ? <div className="speech-bubble" onMouseLeave={this.hoverFalse}
onMouseEnter={this.hoverTrue}>
<span id="heimdall" className={heimdallRendered} onClick={(el) => this.renderEl(el)}>Heimdall</span> <br /> <span className={skadiRendered} id="skadi" onClick={(el) => this.renderEl(el)}>Skadi</span> <br /> <span id="mercator" className={mercatorRendered} onClick={(el) => this.renderEl(el)}>Mercator</span>
</div> : null}
</div>
</li>}
{(this.state.retracted) || ((this.props.history.location.pathname == "/") || (this.props.history.location.pathname == "/register")) || (this.props.history.location.pathname == "/admin") || (this.props.history.location.pathname == "/layout") ? null : <li>
<div className="divicon divicon2 expandicon " onClick={this.props.retract}>
<FontAwesomeIcon className="registerIcon" icon={faArrowAltCircleLeft} />
</div>
</li>}
</ul>
{this.state.retracted ? <div className="expandicon-retracted-container"> <FontAwesomeIcon className="expandicon-retracted" icon={faArrowAltCircleRight} onClick={this.props.unretract} /> </div> : null}
</div>
)
}
}
export default connect(mapStateToProps)(SideBar)
答案 0 :(得分:0)
我做到了
constructor(props) {
super(props);
this.state = {
retracted: this.props.retracted,
isAdmin: false,
isHovering: false,
renderedEl: {
heimdall: false,
skadi: false,
mercator: false
}
}
store.subscribe(() => {
this.setState({
renderedEl: store.getState().renderedEl
}, () =>{
console.log("subscribed")
console.log(this.state.renderedEl)
})
})
this.hoverTrue = this.hoverTrue.bind(this);
this.hoverFalse = this.hoverFalse.bind(this);
}
componentDidMount() {
if (isAdmin()) {
this.setState({
isAdmin: true
})
}
this.setState({
renderedEl: store.getState().renderedEl
})
}