0

I have worked on this problem for quite some time and I am at a lost on how to continue forward. I am trying to return a description of a stock when a certain symbol is entered into the input field. The console.log returns the value of the description when a symbols are entered but it doesn't render it to the page. I have tried to return the whole statement including the map function but that just cancels out my other return statement. I don't know what else to do. Can someone point me in the right direction?

Here is my code:

render() {
        const { stockSymbol, userInput } = this.state




        stockSymbol.map((stock, i) => {

            if (userInput === stock.symbol) {

                return <h2 className="symboldescription" key={i}>
                    {stock.description}
                </h2>,

                    console.log(stock.description + " match")

            }
        })

        return (
            <div className="enterstock">
                <h1 className="title">Enter Stock Symbol</h1>
                <span className="symbol">{this.state.userInput}</span>
                <form className="inputfields" onSubmit={this.getSymbol}>
                    <input type="text" className="symfields" name="symbolname" onChange={this.typeSymbol}></input>
                    <button type="submit" className="btn">Send</button>
                </form>
            </div >
        )

    }
}

2 Answers 2

3

You should be including it as part of the return statement for the render method. For example,

render() {
  const { stockSymbol, userInput } = this.state;

  return (
    <div className="enterstock">
      <h1 className="title">Enter Stock Symbol</h1>
      <span className="symbol">{this.state.userInput}</span>
      <form className="inputfields" onSubmit={this.getSymbol}>
        <input type="text" className="symfields" name="symbolname" onChange={this.typeSymbol}></input>
        <button type="submit" className="btn">Send</button>
      </form>
      {stockSymbol.map((stock, i) => {
        if (userInput === stock.symbol) {
          return <h2 className="symboldescription" key={i}>
            {stock.description}
          </h2>
        }
      })}
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, this solved the problem. I hope you have a wonderful day because you've made mine!
0

the return of your map is not assigned to a variable. You can assign to a variable, and then use in your return of component. something like this:

let description =         stockSymbol.map((stock, i) => {

           if (userInput === stock.symbol) {

               return <h2 className="symboldescription" key={i}>
                   {stock.description}
               </h2>,

                   console.log(stock.description + " match")

           }
       })

and then use in the return of component

        return (
            <div className="enterstock">
                <h1 className="title">Enter Stock Symbol</h1>
                <span className="symbol">{this.state.userInput}</span>
                <form className="inputfields" onSubmit={this.getSymbol}>
                    <input type="text" className="symfields" name="symbolname" onChange={this.typeSymbol}></input>
                    <button type="submit" className="btn">Send</button>
                </form>
                {description}
            </div >
        )

Comments

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.