javascript - 如何单击按钮以获取具有反应的组件之一

标签 javascript html reactjs

这是我的 html文件:

<!DOCTYPE html>
   <html>
     <head>
        <link rel="stylesheet" href="style/style.css">
     </head>
     <body>
        <button id="aa">another</button>
        <button id="eng">English</button>
        <div class="container"></div>
        <div class="containerAA"></div>
     </body>
     <script src="bundle.js"></script>
     <script src="index.js"></script>
     <script src="indexAr.js"></script>
</html>

我想 clickbutton (另一个)它用 class 给 div容器 render functions来自 indexAr.js , 什么时候clickbutton (英文),它用 class 给 div容器AA 那是 render数据来自 index.js .

最佳答案

我认为当您使用 React router 并保留单个 index.js 文件时,问题可以更容易地解决。您无需单独捆绑这些文件。

你的路线将在 index.js 中并且看起来像

import {Router, hashHistory, Route} from 'react-router'
ReactDOM.render(
  <Router history={hashHistory}>
    <Route path="/" component={App}>
        <Route path="another" component={Another}/>
        <Route path="english" component={English}/>
    </Route>
  </Router>, document.getElementById('app')
)

你的 App 组件会是这样的

import {Link} from 'react-router';

class App extends React.Component {
    render() {
      return (
         <div>
             <button><Link to="another">Another</Link><button>
             <button><Link to="english">English</Link></button>    
             <div>{this.props.children}</div>         
         </div>
      )
   }

}

我认为对于您要实现的目标,这是一种更好、 react 更灵敏的方法。

可以关注以下 youtube tutorial 用于设置你的 react 路由器

关于javascript - 如何单击按钮以获取具有反应的组件之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41997805/

相关文章:

html - 显示彼此相邻的列表而不 float

javascript - 简单的 jQuery 脚本遇到问题

reactjs - 使用react-router进行基于身份验证的重定向

javascript - 无法将日期转换为年龄(结果仍然为 NAN)

javascript - Web Worker 中的同步 XHR 请求是否仍会锁定浏览器?

javascript - 在 Parse 平台上将 Canvas 内容保存为图像

javascript - javascript 函数的第二部分未运行?

javascript - 对于网站 : How do I make an image follow the mouse pointer only in a certain area?

javascript - 通过 React js 从具有属性的项目列表中获取随机项目

javascript - 我在 React Component 中的关键实现做错了什么