简体   繁体   English

如何从按钮加载另一个本地js

[英]how to load another local js from a button

When press a button, the browser should reload the page and show a different reactjs component/file. 当按下一个按钮时,浏览器应该重新加载页面并显示一个不同的reactjs组件/文件。

I tried doing things like href="./code.js" or window.location="./code.js" inside the props button. 我尝试在道具按钮内执行诸如href="./code.js"window.location="./code.js"之类的操作。 However, it didn't work. 但是,它没有用。 Now, I'm trying with a function, but it's still not working. 现在,我正在尝试使用一个函数,但仍然无法正常工作。 I'm not really sure how it works. 我不太确定它是如何工作的。

handleClick = () => href="./mycode.js"

export default () => (
  ...
    <Button secondary onClick={this.handleClick}>Registrarse</Button>
  ...);

The error is: 错误是:

./src/Menu.js
  Line 4:   'handleClick' is not defined  no-undef
  Line 4:  'href' is not defined         no-undef

I don't think my implementation is working correctly. 我认为自己的实现无法正常工作。

只需使用React Router处理路由并在页面之间移动即可。

If you want to add another script file, then inside the handleClick create new script tag, set src property and append it to the DOM . 如果要添加另一个脚本文件,则在handleClick创建新的脚本标记,设置src属性并将其附加到DOM

handleClick = () => {
  const script = document.createElement('script');
  script.src = 'code.js';
  document.body.appendChild(script);
}

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

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