Reactjs不允许在onClick

时间:2017-07-20 05:28:44

标签: javascript reactjs

我试图在单击按钮时从ajax函数登录到console.log。单击按钮时没有任何反应。

var Search = () => (

  var ajax() {
        console.log('ajax')
   }

return (
  <div className="search-bar form-inline">
    <input className="form-control" type="text" />
    <button className="btn hidden-sm-down" onClick={() => ajax()} >
      <span className="glyphicon glyphicon-search"></span>
    </button>
  </div> 
)
}

1 个答案:

答案 0 :(得分:1)

您的语法不正确,

你应该:

var Search = () => {

   const ajax = () => {
        console.log('ajax')
   }

return (
  <div className="search-bar form-inline">
    <input className="form-control" type="text" />
    <button className="btn hidden-sm-down" onClick={() => ajax()} >
      <span className="glyphicon glyphicon-search"></span>
    </button>
  </div> 
)
}

&#13;
&#13;
var Search = () => {

   function ajax() {
        console.log('ajax')
   }

return (
  <div className="search-bar form-inline">
    <input className="form-control" type="text" />
    <button className="btn hidden-sm-down" onClick={() => ajax()} >
      <span className="glyphicon glyphicon-search"></span>
    </button>
  </div> 
)
}

ReactDOM.render(<Search/>, document.getElementById('app'));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></app>
&#13;
&#13;
&#13;