使用' this.props.router.push'导航到另一个组件(和页面)的问题。

时间:2017-06-05 00:55:54

标签: javascript html reactjs react-router

我想在单独的组件中单击图像时导航到另一个页面。使用this.props.router.push应该可以实现这一目标。但是,单击图像Uncaught TypeError: Cannot read property 'props' of null时出现此错误。这是我拥有具有点击功能的图像的组件:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { addCart } from '../../actions';
import { withRouter } from 'react-router';
import ProductPage from '


import Shirt from './camodye.jpeg';
import Tape from './Tape.png';
import RegularHat from './regularhat.jpg';


export class WebShop extends Component {
    constructor(props){
        super(props);
        this.state = {value: 'medium', cartData: {} };
        this.handleClick = this.handleClick.bind(this);
        this.change = this.change.bind(this);
    }

    handleClick() {
        let cart = {price:0,item:"userselection",size:this.state.value};
        this.props.onCartAdd(cart);
    } 

    change(e){
        this.setState({value: e.target.value})
    }

    itemSelection(){
        let userOrder = {price:0,item:"",size:""};
        let userItem = "";
        if (userItem == "shirt1") {
           let itemPrice = 20.00;
        }
    }

    goProductPage() {
        this.props.router.push({ProductPage});
    }


    render() {
        console.log('aa');
        return (
            <div className='Webshop' id='Webshop'>
                <ul id="Productlist">
                    <div className='Product'>
                      <img src={Shirt} onClick={this.goProductPage}></img>
                      <button onClick={this.handleClick} className="addit">Add to cart</button>
                      <select id="size" onChange={this.change} value={this.state.value}>
                        <option value="medium">Medium</option>
                        <option value="large">Large</option>
                        <option value="x-large">X-large</option>
                      </select>
                    </div>
                    <div className='Product'>  
                      <img src={RegularHat}></img>
                      <button onClick={this.handleClick} className="addit">Add to cart</button>
                      <select id="size" onChange={this.change} value={this.state.value}>
                        <option value="medium">Medium</option>
                        <option value="large">Large</option>
                        <option value="x-large">X-large</option>
                      </select>
                    </div>
                    <div className='Product'>  
                      <img src={Tape}></img>
                      <button onClick={this.handleClick} className="addit">Add to cart</button>
                      <select id="size" onChange={this.change} value={this.state.value}>
                        <option value="medium">Medium</option>
                        <option value="large">Large</option>
                        <option value="x-large">X-large</option>
                      </select>
                    </div>
                </ul>
            </div>
        );
    }
}

这是我的productpage.js ProductPage组件:

import React, { Component } from 'react';

export default class ProductPage extends  Component {
    render() {
        return (
            <div className= "ProductPage" id="ProductPage">
            </div>
        );
    }

}

为什么我收到此错误?这是做我想做的最好的方式吗?

1 个答案:

答案 0 :(得分:0)

您需要将它绑定到构造函数中的goProductPage方法:

constructor(props){
    super(props);
    this.goProductPage = this.goProductPage.bind(this);
}