[英]React State vs. Props - Why are state changes not reflected in components?
I'm trying to create a div that sits underneath the main app (lexically) but is styled to only show up after a timed delay. 我正在尝试创建一个位于主应用程序(按词法)下方的div,但其样式设置为仅在定时延迟后才显示。 I feel like this is most likely a very simple failure on my part to grasp some of the react concepts I'm working with. 我觉得这很可能是我无法理解我正在使用的一些反应概念的一个非常简单的失败。
Here's my code: (The CSS is pseudo code) 这是我的代码:(CSS是伪代码)
import React, {Component} from 'React'; //eslint-disable-line
import styled from 'styled-components';
import ReactTimeout from 'react-timeout';
const Icon = styled.div.attrs({
dataRight: props => props.dataRight || '1em',
dataLeft: props => props.dataLeft || '1em',
displayIcons: props => props.displayIcons|| 'none'
})`
display: ${props => props.displayIcons};
font-size: 1.5rem;
background-color: rebeccapurple;
position: absolute;
top: 1rem;
right: ${props => props.dataRight};
left: ${props => props.dataLeft};
`;
class Iconset extends Component {
constructor(props) {
super(props);
this.state = {
displayIcons: 'none'
};
}
componentDidMount () {
this.props.setTimeout(this.showIcons, 4000);
alert('Display Icons = ' + this.state.displayIcons);
}
showIcons() {
this.setState({displayIcons: 'Block'});
alert('Display Icons = ' + this.state.displayIcons);
}
render () {
return (
<div id='iconset'>
<Icon dataLeft="auto" dataRight="1em" display={this.props.displayIcons}>First Icon</Icon> {/*eslint-disable-line */}
<Icon dataLeft="1em" dataRight="auto" display={this.props.displayIcons}>Second Icon</Icon> {/*eslint-disable-line */}
</div>
);
}
}
export default ReactTimeout(Iconset);
So, my current understanding is that when the timeout fires the container state change should populate down to the children and override the display: none with display: block. 因此,我目前的理解是,当超时触发时,容器状态更改应填充到子级,并覆盖display:none和display:block。 That change never seems to happen although the state-change itself does happen. 尽管状态改变本身确实发生了,但是这种改变似乎从未发生过。
What concept am I missing here? 我在这里想念什么概念?
When you use setState, you're setting the displayIcons
variable in component's internal state which would be accessed by this.state.displayIcons
. 当使用setState时,您正在将displayIcons
变量设置为组件的内部状态,此状态可以由this.state.displayIcons
访问。
If you look at your render, in the display prop, you're targeting this.props.displayIcons
如果您查看渲染,则在显示道具中,您的目标是this.props.displayIcons
You would only use props here if you were changing the displayIcons
property in a parent component. 如果要更改父组件中的displayIcons
属性,则仅在此处使用props。
Change that to this.state.displayIcons
and it should work as you expect. 将其更改为this.state.displayIcons
,它将按预期工作。
I'm not sure what makes sense or not to your code. 我不确定什么对您的代码有意义。
this.props.setTimeout(this.showIcons, 4000);
Is setTimeout really a props function? setTimeout真的是道具功能吗? It looks like that what you really want was to just call setTimeOut: 看起来您真正想要的只是调用setTimeOut:
setTimeout(this.showIcons, 4000);
Why are you rendering props.displayIcons ? 为什么要渲染props.displayIcons?
<Icon dataLeft="auto" dataRight="1em" display={this.props.displayIcons}>First Icon</Icon> {/*eslint-disable-line */}
<Icon dataLeft="1em" dataRight="auto" display={this.props.displayIcons}>Second Icon</Icon> {/*eslint-disable-line */}
I believe what you really want is to render the state that you changed on timeOut: 我相信您真正想要的是呈现您在timeOut更改的状态:
<Icon dataLeft="auto" dataRight="1em" display={this.state.displayIcons}>First Icon</Icon> {/*eslint-disable-line */}
<Icon dataLeft="1em" dataRight="auto" display={this.state.displayIcons}>Second Icon</Icon> {/*eslint-disable-line */}
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.