简体   繁体   English

如何将 innerHTML 传递给 onClick function (Typescript)

[英]How can I pass innerHTML to an onClick function (Typescript)

I'm trying to pass the Square element's innerHTML to the onClick function. I have also tried to pass in just i but it always is equal to 100. Is there a way to either pass i when it's equal to the same value that goes into the Square or is there a way to pass the innerHTML to the function. Currently, this code generates the error: [TS: 2532]this is possibly undefined I'm making a grid of 100 squares, each one is a button, and each one should have it's own ID/number from 1-100 to identify them.我正在尝试将 Square 元素的 innerHTML 传递给 onClick function。我也尝试只传递i但它始终等于 100。有没有一种方法可以传递i当它等于进入的相同值时Square 或有没有办法将 innerHTML 传递给 function。目前,这段代码会产生错误: [TS: 2532]this is possibly undefined的我正在制作一个由 100 个正方形组成的网格,每个都是一个按钮,每个一个人应该有自己的 ID/编号,从 1-100 来识别它们。 This is what I have currently: Grid of 100 squares arranged in 10x10 formation这就是我目前拥有的:以 10x10 的形式排列的 100 个正方形的网格

export const Square = (props:any) =>{
    i += 1;
    if(i > 100)
    {
        i = 1;
    }
    return(
        <DefaultButton styles={factionMapButton} onClick={()=>onSquareClick(this.innerHTML,props.onClick)}>{i}</DefaultButton>
    );
}

const onSquareClick = (number:any,isOpen:any) => {
    console.log(number);
    const panelContent = document.getElementById("panelContent");
    if(panelContent !== null)
    {
        panelContent.innerHTML = number;
    }
    isOpen();
}

You have quite a few problems.你有很多问题。

  • You should do your best to avoid any in TypeScript, especially not liberally - that defeats the whole purpose of type-checking.您应该尽最大努力避免 TypeScript 中的any内容,尤其是不要随意使用 - 这会破坏类型检查的全部目的。 If you're trying to fix type problems, you should start by also typing everything properly.如果你想解决类型问题,你应该从正确输入所有内容开始。
  • Arrow functions do not have their this altered by the calling context.箭头函数的this不会被调用上下文改变。 If there's no enclosing full-fledged function , the this in an arrow function will be the global object or undefined , both of which are useless to you.如果没有封闭的完整function ,箭头 function 中的this将是全局 object 或undefined ,这两者对您都无用。 Either use a function to capture the this , or, even better, use the click event's currentTarget to get a reference to the clicked button.使用function来捕获this ,或者更好的是,使用单击事件的 currentTarget 来获取对单击按钮的引用。
  • The .innerHTML of an element returns a string, not an element.元素的.innerHTML返回一个字符串,而不是一个元素。 If it contains a string that can be coerced to a number, explicitly coerce it to a number instead.如果它包含可以强制转换为数字的字符串,则显式地将其强制转换为数字。 (If the HTML content is only the string that can be coerced to the number, you should use .textContent instead - only use .innerHTML when deliberately setting or retrieving HTML markup, not plain text) (如果 HTML 内容只是可以强制转换为数字的字符串,您应该改用.textContent - 只有在有意设置或检索 HTML 标记时才使用.innerHTML ,而不是纯文本)
  • A better approach would be to pass down the i to onSquareClick instead of using DOM manipulation - using the closure is much easier更好的方法是将i传递给onSquareClick而不是使用 DOM 操作——使用闭包要容易得多
let i = 1;
export const Square = ({ onClick }: { onClick: () => void }) => {
    i += 1;
    if (i > 100) {
        i = 1;
    }
    return (
        <DefaultButton styles={factionMapButton} onClick={(e) => { onSquareClick(i, onClick); }}>{i}</DefaultButton>
    );
};
const onSquareClick = (number: number, isOpen: () => void) => {
    const panelContent = document.getElementById('panelContent');
    if (panelContent !== null) {
        panelContent.innerHTML = String(number);
    }
    isOpen();
};
  • If you're using React, you should not be using vanilla DOM manipulation like panelContent.innerHTML = number;如果你正在使用 React,你不应该使用像panelContent.innerHTML = number;这样的原始 DOM 操作。 - instead, set React state that the view uses to determine what should exist in that element. - 相反,设置 React state 视图使用它来确定该元素中应该存在的内容。 Something like就像是
// Parent component:
const [panelContentText, setPanelContentText] = useState('');
// expand as needed for the other components in your app...
return <div id="panelContent">{panelContentText}</div>
  <Square setPanelContentText={setPanelContentText} /* and other props */ />

// ...

// Then call the setPanelContentText prop in onSquareClick
const onSquareClick = (number: number, isOpen: () => void, setPanelContentText: React.Dispatch<React.SetStateAction<string>>) => {
    setPanelContentText(String(number));
    isOpen();
};

I'd recommend looking into an introductory React tutorial, it looks like you might benefit from learning the process within React's ecosystem, rather than trying to mishmash your understanding of vanilla JS's DOM with React.我建议查看介绍性的 React 教程,看起来您可能会从 React 生态系统中的学习过程中受益,而不是试图将您对 vanilla JS 的 DOM 的理解与 React 混为一谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM