0

I got useState with a list of strings (currencies symbols like "USD", "EUR" etc.)

const [symbols, setSymbols] = useState<string[]>()

And I need to display it into the select field, right now I'm using this:


    const renderInputPanel = ()  => {
        return (
            <div className='input-panel'>
                <form>
                <select>
                    {symbols.map((symbol) => {
                        <option value={symbol}>symbol</option>
                    })}
                </select>
                </form>
            </div>
        )
    }

    return (<>
        {renderInputPanel()}
    </>)

It says the bug is in {symbols.map((symbol) =>..., but I have no idea what to do with it. Full Error:

TS2322: Type 'void[]' is not assignable to type 'ReactNode'.
  Type 'void[]' is not assignable to type 'ReactFragment'.
    The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
      Type 'IteratorResult<void, any>' is not assignable to type 'IteratorResult<ReactNode, any>'.
        Type 'IteratorYieldResult<void>' is not assignable to type 'IteratorResult<ReactNode, any>'.
          Type 'IteratorYieldResult<void>' is not assignable to type 'IteratorYieldResult<ReactNode>'.
            Type 'void' is not assignable to type 'ReactNode'.
    24 |                 <form>
    25 |                 <select>
  > 26 |                     {symbols.map((symbol) => {
       |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 27 |                         <option value={symbol}>symbol</option>
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 28 |                     })}
       | ^^^^^^^^^^^^^^^^^^^^^^^^
    29 |                 </select>
    30 |                 </form>
    31 |             </div>


ERROR in src/converter/Converter.tsx:26:22

TS2532: Object is possibly 'undefined'.
    24 |                 <form>
    25 |                 <select>
  > 26 |                     {symbols.map((symbol) => {
       |                      ^^^^^^^
    27 |                         <option value={symbol}>symbol</option>
    28 |                     })}
    29 |                 </select>
2
  • 1
    you missed return before <option Commented Aug 5, 2022 at 14:22
  • God damn it. You're right... thanks haha Commented Aug 5, 2022 at 14:23

3 Answers 3

2
  1. add return before <option value={symbol}>symbol</option>

or

  1. delete {} wrapped outside <option value={symbol}>symbol</option>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to return JSX inside map function. Also, initialize the state with empty array first and use key property for iterating any elements.

 const [symbols, setSymbols] = useState<string[]>([])
 const renderInputPanel = ()  => {
    return (
        <div className='input-panel'>
            <form>
            <select>
                {symbols.map((symbol, idx) => {
                    return <option key={idx} value={symbol}>{symbol}</option>
                })}
            </select>
            </form>
        </div>
    )
}

return (<>
    {renderInputPanel()}
</>)

Comments

0

You missed return before <option tag

Object is possibly 'undefined' as description says, it means that a Object is possibly 'undefined' so this can throw an error, there are multiple ways to solve this, I recommend Optional chaining.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

  const [symbols, setSymbols] = useState<string[]>();
  return (
    <div className="input-panel">
      <form>
        <select>
          {symbols?.map((symbol) => {
            return <option value={symbol}>symbol</option>;
          })}
        </select>
      </form>
    </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.