0

I am using react and I have a component where I am setting a constant with an array of strings:

const propsToCheck = ['text', 'type', 'status', 'topic.name', 'class.0.code'];

I am sending down the children chain all the way to the component that is using it like this in the function checkObjectContainsValue that I import from another file:

constructor(props) {
    super(props);
    this.state.data = this.transformRows(props.rows.filter(obj => checkObjectContainsValue(obj, props.searchString)(props.propsToCheck)))
  }

My tests are failing then because I get an error:

TypeError: propsToCheck is not iterable

But, I noticed that if I import the checkObjectContainsValue function to a parent component where I have propsToCheck I don't get the error. But I can't have that function there for other reasons. I have checked the react developer tools and I can see that I am getting the array propsToCheck in the child component, so I am not sure why I am getting this error?

This is the function checkObjectContainsValue:

const checkObjectContainsValue = (obj, value) => (propsToCheck) => {
  for (let prop of propsToCheck) {
    const propToCheckValue = prop.split('.').reduce((currentObject,property) => {
      return currentObject[property];
    }, obj);
    if (propToCheckValue.toLowerCase().includes(value.toLowerCase())) {
      return true;
    }
  }
  return false;
};

Update

If I move the logic to a parent component then everything works fine. This is the example:

onSearchChange(event) {
    const searchString = event.target.value;
    this.setState(prevState => {
      const filteredTasks = prevState.tasks.filter(obj => checkObjectContainsValue(obj, searchString)(propsToCheck));
      return {filteredTasks: filteredTasks};
    });
  }

Why am I getting this error only when I use this function in the child component?

6
  • 2
    is your function definition is like this func()() ? as you're calling checkObjectContainsValue(obj, props.searchString)(props.propsToCheck) ? does checkObjectContainsValue return a function ? Commented May 29, 2019 at 8:00
  • Yes, checkObjectContainsValue returns a function Commented May 29, 2019 at 8:04
  • can we see how checkObjectContainsValue looks like ? Commented May 29, 2019 at 8:05
  • On checking in the browser, everything works fine, and I get propsToCheck in the child component as an array Commented May 29, 2019 at 8:07
  • @Leff you're just passing two arguments to function checkObjectContainsValue whereas it expects three arguments, so the third one becomes undefined , which is not iterable Commented May 29, 2019 at 8:08

1 Answer 1

1

Your function expects three argument

const checkObjectContainsValue = (obj, value, propsToCheck) => {

where as you're just passing two argument to function

props.rows.filter(obj => checkObjectContainsValue(obj, props.searchString)

so the third argument will have a value equal to undefined which is not iterable

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, that was wrong copy pasting, this actually the signature of the function const checkObjectContainsValue = (obj, value) => (propsToCheck) => I was just testing if anything would change if I make it a normal function and not a currying one.
No, propToCheckValue is a value of a prop, which is a string.
@Leff can you show how you're sending props to child ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.