从外部API在React中呈现列表

huangapple go评论73阅读模式
英文:

Rendering a list from external API in react

问题

以下是您提供的代码的中文翻译:

我试图渲染用户点击的某些信息当用户点击时他们将看到代码列表然后获取额外信息

让我们从头开始

首先我使用了`useState` Hook来初始化选项

然后我从外部API获取了一些数据将数据转换为JSON格式并更新了选项然后我将存储在选项中的数据映射到选项标记然后在选择标记中返回这些选项然后我添加了一个名为`handleClick`的选择标记的事件处理程序然后我使用过滤器来获取特定选项问题在于当我尝试显示信息时它并没有显示出来我尝试使用`console.log`它运行得很完美但当我尝试在列表中显示它时什么都不显示

以下是我的代码

```javascript
import { useEffect, useState } from "react";

function Data() {
    // 使用useState Hook初始化选项
    const [options, setOption] = useState([]);

    useEffect(() => {
        // 获取数据
        fetch('https://api.n.exchange/en/api/v1/currency/')
            .then(response => response.json())
            .then(data => setOption(data));
    });

    function handleClick(event) {
        const selectedBase = options.filter(code =>
            code.code === event.target.value
        );
        return (
            <ul>
                {selectedBase.map(currency => <li>{currency.name}</li>)}
            </ul>
        );
    }

    return (
        <select onChange={handleClick}>
            <option value="" hidden>Select a Currency</option>
            {options.map(option => (
                <option key={option.code} value={option.code}>
                    {option.code}
                </option>
            ))}
        </select>
    );
}

export default Data;

请注意,这是您提供的代码的中文翻译,没有其他内容。

英文:

I'm trying to render the some information that the user clicked on. The user will see a list of codes when clicked upon they just get additional information.

Lets start from the top.

So firstly I used useState Hook to initialize the options.
Then I fetched some data from an external API, converted the data into JSON format and updated options. I then mapped the data that was store in options each into a option tag and returned the options in a select tag. I then added an event handler to the select tag called handleClick. I then used filter to get the specific option. The problem is that when I am trying to display the information but it isn't display. I tried to console.log it and it worked perfect but when I try to display it in a list nothing comes up.

Here is my code:

import { useEffect,useState } from &quot;react&quot;
function Data() {
//Intializing Options using the useState Hook
const [options , setOption] = useState([])
useEffect(() =&gt; {
//Fetching the data
fetch(&#39;https://api.n.exchange/en/api/v1/currency/&#39;)
.then(response =&gt; response.json())
.then(data =&gt;
setOption(data)
)
})
function handleClick(event) {
const selectedBase = options.filter(code =&gt;
code.code === event.target.value
)
return(
&lt;ul&gt;
{selectedBase.forEach(currency =&gt; &lt;li&gt;{currency.name}&lt;/li&gt;)}
&lt;/ul&gt;
)
}
return(
&lt;select onChange={handleClick}&gt;
&lt;option select=&quot;true&quot; hidden={true}&gt;Select an Currency&lt;/option&gt;
{
options.map(options =&gt;
&lt;option key={options.code} value={options.code}&gt;{options.code}&lt;/option&gt;
)
}
&lt;/select&gt;
)
}
export default Data

答案1

得分: 0

const [optionSelected, setOptionSelected] = useState(0)

function handleClick(){
 const selectedBase = options.filter(code =>
                code.code === event.target.value
        )
 setOptionSelected(selectedBase)
}

<><select onChange={handleClick} value={optionSelected}>
    <option select="true" hidden={true}>选择一种货币</option>
    {
        options.map(option =>
            <option key={option.code} value={option.code}>{option.code}</option>
        )
    }
</select>
<ul>
   // 从optionSelectedState映射的数据
</ul>
</></>
英文:

It's better to create a state to store option selected

const [optionSelected, setOptionSelected] = useState(0)

Then modify your handle change to only just set state option selected not return the jsx, something like this:

function handleClick(){
const selectedBase = options.filter(code =&gt;
code.code === event.target.value
)
setOptionSelected(selectedBase)
}

At the return jsx of your component the reason not showing because you are not storing the value after your choose option, modify like this:

&lt;&gt;
&lt;select onChange={handleClick} value={optionSelected}&gt;
&lt;option select=&quot;true&quot; hidden={true}&gt;Select an Currency&lt;/option&gt;
{
options.map(options =&gt;
&lt;option key={options.code} value={options.code}&gt;{options.code}   
&lt;/option&gt;
)
}
&lt;/select&gt;
&lt;ul&gt;
//your mapping data from optionSelectedState
&lt;/ul&gt;
&lt;/&gt;

huangapple
  • 本文由 发表于 2023年4月4日 10:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定