ReferenceError,因为未传递变量

时间:2018-12-10 14:23:29

标签: javascript reactjs react-native react-redux

在我的本地本机应用程序中,我有一个ProductScreen,其中导入了两个文件以完成屏幕。

文件1(Products.js)是我的产品列表

文件2(Data.js)是我的数据文件(只是临时文件)

 import Products from '../../components/Products';
 import { shoes} from '../../Data';

在我的ProdcutScreen中,我将导入的Products用作component,并将products的值设置为shoes

<Products products={shoes} onPress={this.props.addItemToCart}/>

然后在Product.js文件中,我尝试将产品列表中的state设置为产品(在ProdcutScreen中调用时,将其设置为shoes

state = {
    products, 
    filteredProducts: products,
  };

这是问题起作用的地方,因为出现错误

ReferenceError: ReferenceError: ReferenceError: ReferenceError: Can't find variable: products

问题来自Products.js文件

中的第16行
state = {
        products, 
        filteredProducts: products,
      };

因此看来,从shoes文件收集的值Data.js并没有传递给状态。


我的问题是如何传递此值的?

当我不设置state而只是这样渲染列表时,列表确实起作用

{this.renderProducts(this.props.products)}

但是我需要设置状态,因为我希望能够过滤我的产品。


ProductScreen.js

 import { drinks } from '../../Data';
 import { connect } from 'react-redux';

export  class ProductScreen extends React.Component {
    static navigationOptions = {
        headerTitle: 'shoes'
}




      render() {
        return (

            <View style={styles.container}>
                <Products products={shoes} onPress={this.props.addItemToCart}/> 
                <View >
                    {/* <TouchableOpacity style={styles.checkOutContainer} onPress={() => this.props.onPress(item)} > 
                        <Icon style={styles.checkmark} name="ios-checkmark" color="white" size={35} />
                    </TouchableOpacity> */}
                </View>
            </View>

        )
      }
    }

Data.js

export const shoes= [
    {
        id: 1,
        name: 'Hurrace',
        brand: 'Nike',
        type: 'strong',
        price: 7,
    },
]

Products.js

class Products extends Component {

    state = {
        products, 
        filteredProducts: products,
      };

      setSearchText(event) {
        const searchText = event.nativeEvent.text;

        const textLength = this.state.products.length;

        const filteredTexts = this.state.products.filter(row => {
          return row.name.indexOf(searchText) !== -1;
        });
        console.log("text: " + JSON.stringify(filteredTexts));

        this.setState({
          searchText,
          filteredProducts: filteredTexts
        });
      }

    renderProducts = (products) => {
        console.log(products)

        return products.map((item, index) => {
            return (
                <View key={index} style={styles.shoes}>

                    <View style={styles.text}>
                        <Text style={styles.name}>
                            {item.name}
                        </Text>

                        <Text style={styles.price}>
                        € {item.price}
                        </Text>
                    </View>
                    <View style={styles.buttonContainer}>
                        <TouchableOpacity onPress={() => this.props.onPress(item)} > 
                        <Icon style={styles.button} name="ios-add" color="white" size={25} />
                        </TouchableOpacity>
                    </View>
                </View>
            )
        })
    }

几个月后,我仍在学习本机反应。

1 个答案:

答案 0 :(得分:1)

如果打算使用prop作为初始值,则不能使用实例属性。您需要通过构造函数(要传递道具的地方)进行操作:

NODE_ENV=stage