React Uncaught ReferenceError:未定义变量

时间:2016-09-06 21:36:50

标签: reactjs

我使用一个组件2次。当它第一次调用evering是好的但是当我第二次调用时(通过反应路由器移动到不同的组件)我有异常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>
    )
}
}

1 个答案:

答案 0 :(得分:2)

您打算将button包含在发生第二次通话的文件TitleWithAddButton中,但包含AddCarWashPage.jsx