2

I want to convert an array prop into a string:

   export default class MyComp extends React.Component {
    render() {
        let type = this.props.type; // [".abc",".ded",".ted"];
        newType = type.join();
//o/p: newType= ".abc,.ded,.ted"

console.log(newType) // ".abc,.ded,.ted"
    return (
      <div>
      <input type="file" accept={newType}/> //here throws error 
    </div>
    )

    }

}

export default class SubComp extends React.Component{
   render() {
     <Mycomp type={[".abc",".ded",".ted"]}/>

 }
}

when I try to access newType as a values to the accept html, it throws: TypeError: Cannot read property 'join' of undefined. One thing I checked here if I try to pass the hard code values to newType it gets working fine. Only when Im trying to convert the array to string using .join() or .toString(), it fails.

render() {
            let type = this.props.type; // [".abc",".ded",".ted"];
            newType = ".abc,.ded,.ted";
        return (
          <div>
          <input type="file" accept={newType}/> //Works Fine!!!! 
        </div>
        )

        }

Any idea what may be causing the issue?

10
  • 1
    did you console.log out type? after getting it from the props? Commented Oct 10, 2018 at 20:51
  • yes, the type is an array and newType get converted to string Commented Oct 10, 2018 at 20:53
  • please share the code from where you are passing props. it seems like type is undefined. Commented Oct 10, 2018 at 20:53
  • Use a linter... Commented Oct 10, 2018 at 21:04
  • 1
    Did you notice you have a typo? "newType" vs "newtype" Commented Oct 10, 2018 at 21:05

4 Answers 4

1

There are actually two problems with your code in the example:

  1. You have to use curly brackets around prop values that aren't strings, so it should look like this: <Mycomp type={[".abc",".ded",".ted"]} />
  2. You assign the joined array to newType, but then use newtype in your input tag (note the capitalization difference)

The corrected code would look like this:

export default class MyComp extends React.Component {
  render() {
    let type = this.props.type;
    let newType = type.join();
    return (
      <div>
        <input type="file" accept={newType}/>
      </div>
    )
  }
}

export default class SubComp extends React.Component {
  render() {
    <Mycomp type={[".abc",".ded",".ted"]} />
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set your type prop using curly brackets {}, like so:

 <Mycomp type={[".abc",".ded",".ted"]}/>

Comments

1

Try to pass the array to prop like this

<Mycomp type={[".abc",".ded",".ted"]} />

Comments

1

You should pass data to props like this

export default class MyComp extends React.Component {
    render() {
        let type = this.props.type; // [".abc",".ded",".ted"];
        let newType = type.join()//converts to string: ".abc,.ded,.ted"
    return (
      <div>
      <input type="file" accept={newType}/> //here throws error 
    </div>
    )

    }

}

export default class SubComp extends React.Component{
   render() {
     <Mycomp type={[".abc",".ded",".ted"]}/>

 }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.