[英]Error mensage “state is not defined” in Meteor + React.
Which is wrong with this function, I try to fade the Navbar at the time of scrolling but it throws me an error in the following function? 这个功能有什么问题,我尝试在滚动时淡化导航栏,但是在以下功能中却抛出了错误?
const opacity = Math.min(100/state.alturaActualScroll, 1);
i have my navbar component but tells me that the state has not been defined but I do not know because the error, this is my code 我有我的导航栏组件,但告诉我该状态尚未定义,但我不知道因为错误,这是我的代码
import React from 'react';
import {Link} from 'react-router'
import { Accounts } from 'meteor/accounts-base';
import {Meteor} from 'meteor/meteor';
export default class NavbarLanding extends
React.Component {
componentDidMount() {
window.onscroll =()=> {
this.setState({
alturaActualScroll: window.scrollY})
}
}
componentDidMount () {
window.onscroll =()=>{
nuevaAlturaScroll = Math.ceil(window.scrollY / 50) *50;
if(this.state.alturaActualScroll != nuevaAlturaScroll){
this.setState({alturaActualScroll:nuevaAlturaScroll})
}
}
}
// update
render() {
// console.log('donde estoy');
const opacity = Math.min(100/state.alturaActualScroll, 1);
return(
<div style={opacity}id="navbar"className="navbar-landing">
<nav>
<div>
<ul className="ul-landing">
{/* <img src="./public/images/flat-rocket.jpg"></img> */}
<li id="navbar-landing-title" className="navbar-title"><a>Landing </a></li>
<div id="menu-landing"className="navbar-menu">
<li><a>acerca</a></li>
<li><a>portafolio</a></li>
<li><a>contacto</a></li>
<button className="btn"onClick={() => Accounts.logout()}>Logout</button>
</div>
</ul>
</div>
</nav>
</div>
);
};
}
NavbarLanding.reactProptype = {
title: React.PropTypes.string.isRequired
};
Your code should be 您的代码应为
const opacity = Math.min(100/this.state.alturaActualScroll, 1);
You forgot to add the this
keyword while accessing the state of the component, which led the interpreter to look for a local variable named state instead of the class level state variable. 您忘记了在访问组件状态时添加了
this
关键字,这导致解释器寻找名为state的局部变量而不是类级别的state变量。
Edited and corrected after comments from Felix Kling and zerkms 根据Felix Kling和zerkms的评论进行编辑和更正
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.