简体   繁体   English

我可以在组件中访问的道具无法呈现

[英]Prop I can access in component can't be rendered

My MERN app's purpose is to take a 3-digit input from a user and return the first prime number that contains those digits (ex: you enter 026, the app returns 10267). 我的MERN应用的目的是从用户那里输入3位数字,然后返回包含这些数字的第一个素数(例如:您输入026,该应用返回10267)。 If my checker() function finds a match, I run updatePrimeNumber() to update that prime number's "match" variable to true. 如果我的checker()函数找到一个匹配项,则运行updatePrimeNumber()将该素数的“ match”变量更新为true。 Database is updating properly. 数据库正在正确更新。

I want to display all prime numbers that have "match" equal to true. 我想显示所有具有“ match”等于true的质数。 I have a prop, primeNumbers , which contains all the primes I have entered into the database. 我有一个primeNumbers ,其中包含我输入到数据库中的所有素数。 I can access those numbers throughout my component, but I can not figure out how to render any of them, true or not. 我可以在整个组件中访问这些数字,但是我无法弄清楚如何渲染其中的任何一个,无论正确与否。 The error I receive is that primeNumbers is not defined . 我收到的错误primeNumbers is not defined

import React, { Component } from 'react';
import gql from "graphql-tag";
import { graphql, compose } from 'react-apollo';

const PrimeNumbersQuery = gql`
{
  primeNumbers {
    id
    text
    match
  }
}
`;

const UpdateMutuation = gql`
  mutation($id: ID!, $match: Boolean!) {
    updatePrimeNumber(id: $id, match: $match)
  }
`;

class Prime extends Component {
  checker = (primes) => {
    let nums = this.props.checkedNumbersFromParent;
    let recentChecked = (nums[nums.length - 1].text);
    let recentCheckedRegExp = new RegExp(recentChecked);
    for (var i in primes) {
      let count = 0;
      if (primes[i].text.search(recentCheckedRegExp) >= 0 && count < 1) {
        this.updatePrimeNumber(primes[i]);
        count = count + 1;
        break;
      }
    }
    return primes;
  }

  updatePrimeNumber = async (primeNumber) => {
    await this.props.updatePrimeNumber({
      variables: {
        id: primeNumber.id,
        match: primeNumber.match
      },
      update: store => {
        const data = store.readQuery({ query: PrimeNumbersQuery });
        data.primeNumbers = data.primeNumbers.map(
          x =>
            x.id === primeNumber.id
            ? {
                ...primeNumber, match: true
              }
            : x
        );
        store.writeQuery({ query: PrimeNumbersQuery, data });
      }
    });
  };

  render() {
    const {data: {loading, primeNumbers}} = this.props;
    this.checker(primeNumbers)
    return (
      <div>
      ***Need to render all primeNumbers with match === true***
      </div>
    );
  }
}

export default compose(
  graphql(UpdateMutuation, {name: 'updatePrimeNumber'}),
  graphql(PrimeNumbersQuery)
)(Prime);

Thank you to anyone who looks at this. 谢谢任何看过这个的人。 It is much appreciated. 非常感谢。

About your problem:primeNumbers is not defined.UpdatePrimeNumber is async, you need to check it first and add constructor to initial this.props. 关于您的问题:没有定义primeNumbers.UpdatePrimeNumber是异步的,您需要首先检查它并将构造函数添加到初始this.props中。

 constructor(){
      super(props)
    }
 renderprime(){
    this.checker(this.props.data.primeNumbers)
    if (this.props.data.primeNumbers){
        return (
            <div>
                {this.props.data.primeNumbers.map((item) => {
                    if (item.match){
                        return (
                            <div>
                                <div>{item.id}</div>
                                <div>{item.text}</div>
                            </div>
                        )
                    }
                })}
            </div>
        )
    }
}
...
   <div>
     {this.renderprime()}
  </div>

which line is the error being thrown from ? 从哪一行抛出错误?

You can try using the filter instead of updating the match in the primeNumber. 您可以尝试使用过滤器,而不是更新primeNumber中的匹配项。

to render the prime numbers replace Need to render all primeNumbers with match === true loop through the primeNumbers 渲染素数替换需要渲染所有具有match === true的primeNumber遍历primeNumbers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM