Javascript 不确定交叉原点错误的来源

Javascript 不确定交叉原点错误的来源,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,出于某种原因,添加行{crooks.map(crook=>crook.setup)}(参见下面的完整组件)会引发错误(如下所示)。我觉得这与useState挂钩有关,但我不确定该怎么做。任何帮助都将不胜感激 组件代码如下: import React, {useState, useEffect} from 'react' function JokeFinder() { //2 states: input query, jokes shown const [query, setQ

出于某种原因,添加行
{crooks.map(crook=>crook.setup)}
(参见下面的完整组件)会引发错误(如下所示)。我觉得这与useState挂钩有关,但我不确定该怎么做。任何帮助都将不胜感激

组件代码如下:

import React, {useState, useEffect} from 'react'

function JokeFinder() {

    //2 states: input query, jokes shown
    const [query, setQuery] = useState('')
    const [jokes, setJokes] = useState([])
    
    const getJokes = async (event) => {
        
        event.preventDefault();
        let URL;
        
        try{
            if(query === "one" || query === "1") {
                URL = `https://official-joke-api.appspot.com/jokes/random`
            } else if (query === "ten" || query === "10") {
                URL = 'https://official-joke-api.appspot.com/jokes/ten'
            }
            const result = await fetch(URL);
            const data = await result.json();
            setJokes(data);
        } catch(err) {
            console.log(err);
        }
    }
    
    const changeHandler = (event) => {
        setQuery(event.target.value)
    }
    
    return(
        <div>
            <form className="form" onSubmit={getJokes}>
                <label className="label" htmlFor="query">It's never a bad time to have a laugh! How many jokes would you like to enjoy? One or ten? Type in the number below.</label>
                <input 
                    className="input"
                    type="text"
                    name="query"
                    placeholder="Enter number of Jokes Desired"
                    value={query}
                    onChange={changeHandler}
                />
                <button className="button" type="submit">Search!</button>
            </form>
            <div className="card-list">
                {jokes.map(joke => joke.setup)}
            </div>
        </div>
    )
}

export default JokeFinder
import React,{useState,useffect}来自“React”
函数JokeFinder(){
//2个状态:输入查询,显示笑话
const[query,setQuery]=useState(“”)
常量[笑话,设置笑话]=useState([])
const getjones=async(事件)=>{
event.preventDefault();
让URL;
试一试{
如果(查询==“一”| |查询==“1”){
URL=`https://official-joke-api.appspot.com/jokes/random`
}else if(查询==“十”| |查询==“10”){
URL='1〕https://official-joke-api.appspot.com/jokes/ten'
}
const result=等待获取(URL);
const data=wait result.json();
(数据);
}捕捉(错误){
控制台日志(err);
}
}
常量changeHandler=(事件)=>{
setQuery(event.target.value)
}
返回(
现在是开怀大笑的好时机!你喜欢听多少个笑话?一个还是十个?输入下面的数字。
搜索!
{crooks.map(crook=>crook.setup)}
)
}
导出默认JokeFinder
遇到的错误如下:

!Error: Unknown error
!The above error occurred in the <JokeFinder> component: in JokeFinder (created by Main) in div (created by Main) in Main Consider adding an error boundary to your tree to customize error handling behavior.
>Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development.
!错误:未知错误
!上面的错误发生在组件中:在DoV(由main创建)中的JokFiFER(由main创建)中的main考虑添加一个错误边界到您的树来定制错误处理行为。
>错误:引发了一个跨原点错误。React无法访问开发中的实际错误对象。

您错误地使用了映射功能。您应该将一个函数传递给map

const{useState}=React;
const JokeFinder=()=>{
const[query,setQuery]=useState(“”);
常量[笑话,设置笑话]=useState([]);
const get笑话=事件=>{
event.preventDefault();
让URL;
试一试{
如果(查询==“一”| |查询==“1”){
URL=`https://official-joke-api.appspot.com/jokes/random`;
}else if(查询==“十”| |查询==“10”){
URL=”https://official-joke-api.appspot.com/jokes/ten";
}
获取(URL)。然后((结果)=>{
然后(data=>setjones(data));
});
}捕捉(错误){
控制台日志(err);
}
};
const changeHandler=事件=>{
setQuery(event.target.value);
};
返回(
开怀大笑从来都不是个坏时机!你想要多少个笑话
要享受?一个或十个?请输入以下数字。
搜索!
{笑话。长度(
笑话。地图((笑话,索引)=>(

{joke.setup}

{chake.punchline}

)) ) : (

{crooks.setup}

{笑话.punchline}

)} ); }; 类应用程序扩展了React.Component{ render(){ 返回( ); } } ReactDOM.render(,document.getElementById('root'))
。输入容器{
边缘顶部:10px;
}
.输入容器输入{
右边距:10px;
最小宽度:240px;
}
.设置{
字号:700;
}
punchline先生{
左侧填充:10px;
}

您错误地使用了映射功能。您应该将一个函数传递给map

const{useState}=React;
const JokeFinder=()=>{
const[query,setQuery]=useState(“”);
常量[笑话,设置笑话]=useState([]);
const get笑话=事件=>{
event.preventDefault();
让URL;
试一试{
如果(查询==“一”| |查询==“1”){
URL=`https://official-joke-api.appspot.com/jokes/random`;
}else if(查询==“十”| |查询==“10”){
URL=”https://official-joke-api.appspot.com/jokes/ten";
}
获取(URL)。然后((结果)=>{
然后(data=>setjones(data));
});
}捕捉(错误){
控制台日志(err);
}
};
const changeHandler=事件=>{
setQuery(event.target.value);
};
返回(
开怀大笑从来都不是个坏时机!你想要多少个笑话
要享受?一个或十个?请输入以下数字。
搜索!
{笑话。长度(
笑话。地图((笑话,索引)=>(

{joke.setup}

{chake.punchline}

)) ) : (

{crooks.setup}

{笑话.punchline}

)} ); }; 类应用程序扩展了React.Component{ render(){ 返回( ); } } ReactDOM.render(,document.getElementById('root'))
。输入容器{
边缘顶部:10px;
}
.输入容器输入{
右边距:10px;
最小宽度:240px;
}
.设置{
字号:700;
}
punchline先生{
左侧填充:10px;
}