【问题标题】:React Uncaught ReferenceError: variable is not definedReact Uncaught ReferenceError:未定义变量
【发布时间】:2016-09-07 05:42:33
【问题描述】:

我使用一个组件 2 次。当它第一次调用时很好但是当我第二次调用时(通过反应路由器移动到不同的组件)我有异常Uncaught ReferenceError: titleStyle is not defined。在控制台中,我看到了这一行的问题:_react2.default.createElement("h2", { style: titleStyle },this.props.title,":") 我做错了什么?

TitleWithAddButton.jsx(有问题的组件)

import React from 'react';
import {Link} from 'react-router'

export default class TitleWithAddButton extends React.Component{
 render(){
    let titleStyle = {
        width:"50%"
    };

     var button = {
         width: "10%",
         float: "right"
     };

    return (
        <div className="title-with-add-button">
            <div>
                <Link to="/carwashAdd"><button type="button" className="btn btn-success" style={button}>Add</button></Link>
            </div>
            <h2 style={titleStyle}>{this.props.title}:</h2>
        </div>
    )
}
}

CarWashPage.jsx(第一次调用组件)

import React from 'react';
import TitleWithAddButton from './TitleWithAddButton.jsx';
import AllCarWashTable from './carwash/AllCarWashTable.jsx'

export default class CarWashPage extends React.Component{

render(){
    var carWashPageStyle = {
        paddingLeft: 10,
        paddingRight: 10
    }

    return (
       <div style={carWashPageStyle}> 
            <TitleWithAddButton title="All carwash"/>
            <AllCarWashTable/>
       </div>    
    )
}
}

AddCarWashPage.jsx(第二次从这里调用组件)

import React from 'react';
import Title from './../Title.jsx'

export default class AddCarWashPage extends React.Component{

render(){
    var addCarWashPage = {
        paddingLeft: 10,
        paddingRight: 10
    }

    return (
        <div style={addCarWashPage}>
            <Title title="Add CarWash"/>
        </div>
    )
}
}

【问题讨论】:

  • 您可以为您的Title.jsx 组件添加代码吗?

标签: reactjs


【解决方案1】:

您的本意是将TitleWithAddButton 包含在文件AddCarWashPage.jsx 中,您的第二次调用发生在该文件中,但改为包含import Title from './../Title.jsx'

【讨论】:

  • 我认为这不是问题所在。它可能是一个不同的模块,加上这是组件内的ReferenceError
  • 嗯,很公平。让我们看看他说了什么,如果猜测不正确,我将删除我的答案。
  • 等等,你实际上可能是对的,正如 OP 所说,他们在 AddCarWashPage 中第二次调用它。如果是这样的话,我会说,标记为错字。
  • 另见AddCarWashPage.jsx 在他的文件结构中自然会比AddCarWash.jsx 深1 级,AddCarWash.jsx 与假定的Title.jsxTitleWithAddButton.jsx 完全相同。感谢您的建议,如何将问题标记为错字?
  • 问题下方有一个按钮,上面写着“标志”。转到“应该关闭”,然后是“离题”,最后是“禁止复制或拼写错误”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2022-11-21
相关资源
最近更新 更多