Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React TypeScript:onClick在地图中_Reactjs_Typescript - Fatal编程技术网

Reactjs React TypeScript:onClick在地图中

Reactjs React TypeScript:onClick在地图中,reactjs,typescript,Reactjs,Typescript,在地图中进行onClick时出现的错误是 Type 'void' is not assignable to type '((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined'.ts(2322) index.d.ts(1348, 9): The expected type comes from property 'onClick' which is declared here on type '

在地图中进行onClick时出现的错误是

Type 'void' is not assignable to type '((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) | undefined'.ts(2322)
index.d.ts(1348, 9): The expected type comes from property 'onClick' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'
类型“void”不能分配给类型“((事件:MouseEvent)=>void)|未定义”。ts(2322)
index.d.ts(1348,9):预期的类型来自属性“onClick”,该属性在类型“DetailedHTMLProps”上声明
向下滚动到“渲染”部分以查看地图中的onClick。。。 也许有更好的方法重定向用户,也解决了这个问题

import React, { useContext, useEffect, useState } from 'react';
import { Global } from '../globalState';

const Followers: React.FC = () => {
  const { global } = useContext(Global);
  const username = window.location.pathname.substr(1);

  interface Follower {
    fullName: string;
    username: string;
    avatarId?: string;
  }

  const [state, setState] = useState({
    followers: [],
  });
  const { followers } = state;

  useEffect(() => {
    async function getFollowers() {
      const response = await fetch(`${global.env.apiUrl}/user/followers`, {
        body: JSON.stringify({ username }),
        headers: {
          // prettier-ignore
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        method: 'POST',
      });
      const content = await response.json();
      if (response.status === 200) {
        setState((prev) => ({ ...prev, followers: content.followers }));
      }
    }
    if (username) {
      getFollowers();
    }
  }, [username, global.env.apiUrl]);

  const gotoUser = (user: string) => { // <------------- onClick calls this function
    window.location.href = (`/${user}`);
  };
  const getAvatar = (fullName: string) => {
    return (
      global.env.imgUrl +
      'initials/' +
      fullName.charAt(0).toLowerCase() +
      '.png'
    );
  };
  return (
    <div className='followers'>
      <h2>Followers</h2>

      {followers.map((person: Follower) => ( // <------------- Map starts
        <div className='followers--grid'>
          <div onClick={gotoUser(person.username)}> // <------------- onClick 
            {person.avatarId && (
              <img
                src={global.env.imgUrl + person.avatarId}
                className='followers--image'
              />
            )}
            {!person.avatarId && (
              <img
                src={getAvatar(person.fullName)}
                className='followers--image'
              />
            )}
          </div>
          <div>
            <p className='followers--name'>{person.fullName}</p>
            <p className='followers--username'>@{person.username} </p>
          </div>
          <div className='followers--follow'>+ Follow</div>
        </div>
      ))}
    </div>
  );
};

export { Followers };
import React,{useContext,useffect,useState}来自'React';
从“../globalState”导入{Global};
const Followers:React.FC=()=>{
const{global}=useContext(全局);
const username=window.location.pathname.substr(1);
接口跟随器{
全名:字符串;
用户名:字符串;
阿凡达?:字符串;
}
常量[状态,设置状态]=使用状态({
追随者:[],
});
const{followers}=状态;
useffect(()=>{
异步函数getFollowers(){
const response=wait fetch(`${global.env.apirl}/user/followers`{
正文:JSON.stringify({username}),
标题:{
//漂亮的忽略
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
方法:“POST”,
});
const content=wait response.json();
如果(response.status==200){
setState((prev)=>({…prev,followers:content.followers}));
}
}
如果(用户名){
getFollowers();
}
},[username,global.env.apirl]);
const gotoUser=(用户:字符串)=>{//{
返回(
global.env.imgUrl+
“缩写/”+
fullName.charAt(0.toLowerCase())+
“.png”
);
};
返回(
追随者
{followers.map((person:Follower)=>(//试试这个:

<div onClick={()=>gotoUser(person.username)}> 
gotoUser(person.username)}>
onClick
需要一个函数。
gotoUser()
不返回任何内容(未定义)。这就是出现错误的原因。创建一个新的箭头函数或使用不带括号的
gotoUser
传递函数本身,而不是函数的返回值