简体   繁体   English

innerHtml中的onclick元素与react

[英]onclick element in innerHtml with react

In a component, i call a funtion to implement this action: ..... 在一个组件中,我调用一个函数来实现此操作:.....

  {
    .....
    .....
    const functionExam = () => {}
    functionExam = function(){}
    .......
    ......
    node.innerHtml = `<div onclick="functionExam">AAAAAAAAAA</div>`;
    ....
    ....
    }
}

functionExam = () => {}
render(){.....

...... ......

So, I want when click on AAAAAA, it will call fuctionExam, but it has an error: 所以,我要单击AAAAAA时,它将调用fuctionExam,但是它有一个错误:

-----Uncaught TypeError: functionExam is not a function
    at HTMLSpanElement.functionExam
------functionExam is not a function
------functionExam is not defined...
...., i tried every way but it still didn'd, please help me.Thank you so much.

That's because your functionExam live in your file, not in global (window) scope. 那是因为您的functionExam位于文件中,而不位于全局(窗口)范围内。 First of all you shouldn't use innerHTML when using React. 首先,在使用React时,您不应该使用innerHTML。 Make a React component and then listen from it for events. 制作一个React组件,然后从中侦听事件。

function MyDiv() {
  return (<div onClick={functionExam}>AAAAAAAAAA</div>);
}

function functionExam () {
  // things happen
}

As there are two types of component in react such as Functional and Class component. 由于react中有两种类型的组件,例如Functional和Class组件。
1. Functional component then follow the below code. 1.功能组件,然后遵循以下代码。

function ActionComponent() {
  function functionExam() {
    console.log('The inner html was clicked.');
  }

  return (
    <div onClick={handleClick}>AAAAAAAAA</div>
  );
}

2. Class Component are as follows. 2.类组件如下。

class ActionComponent extends React.Component {
  constructor(props) {
    super(props);

    // This binding is necessary to make `this` work in the callback
    this.functionExam = this.functionExam.bind(this);
  }

  functionExam() {
    console.log('The inner html was clicked.');
  }

  render() {
    return (
      <div onClick={this.functionExam}>AAAAAAAAA</div>
    );
  }
}

If calling bind annoys you, there are two ways to get rid of this by using arrow function . 如果调用bind惹恼了您,可以通过两种方法使用arrow function摆脱这种情况。

class ActionComponent extends React.Component {

  // This syntax ensures `this` is bound within functionExam.
  functionExam = () => {
    console.log('The inner html was clicked.');
  }

  render() {
    return (
      <div onClick={this.functionExam}>AAAAAAAAA</div>
    );
  }
}

Another way of using a arrow function is as follows: 使用箭头功能的另一种方法如下:

class ActionComponent extends React.Component {

  functionExam() {
    console.log('The inner html was clicked.');
  }

  render() {
    return (
      <div onClick={()=>this.functionExam}>AAAAAAAAA</div>
    );
  }
}

Note : 注意 :

  1. You can put any valid JavaScript expression inside the curly braces in JSX 您可以将任何有效的JavaScript表达式放在JSX的花括号
  2. React uses the camel case. React使用骆驼盒。 So use onClick instead of onclick. 因此,请使用onClick而不是onclick。
  3. Don't use innerHTML in react. 不要在反应中使用innerHTML。

Try to make functionExam within the component(ie function or class component) or at the parent level which can have access through props. 尝试在可以通过道具访问的组件(即函数或类组件)内或在父级上进行functionExam。
Please let me know weather I am up to the point of your question. 请让我知道天气,直到您提出问题为止。

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

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