Abhilash Muttalli
Abhilash Muttalli

Reputation: 1369

onclick and function calling using reactjs

when ever i click the student button it should call the function inside that i have called the other component, but i am getting error. what should i do to call other component by onlick .

   import React from 'react';
//import s from './style.css';
import Students from './students.js'
require('./style.scss');
class Display extends React.Component {

  function  studentfunction(){
  <Students/>
  };
  render()
  {

    return(
      <div className="container">
      {this.state.clicked ? <Students/>}
      <h1 className="dashboard">Dashboard</h1>
     <table className="table1" >
     <tbody className="tbody1" >
<tr>
    <td ><input  type="button" value="Students" onClick={this.studentfunction} /></td>
    <td> <input type="button" value="Teachers" /></td>

    <td> <input type="button" value="Inbox" /></td>
</tr>
<tr>
   <td> <input type="button" value="Holidays" /></td>
    <td> <input type="button" value="Circular" /></td>

    <td> <input type="button" value="Notifications" /></td>
    </tr>
<tr>
    <td> <input type="button" value="Events" /></td>

      <td> <input type="button" value="Gallery" /></td>

</tr>
</tbody>
    </table>

      </div>
    );
  }
}
export default Display

;

Upvotes: 1

Views: 894

Answers (2)

Diego Faria
Diego Faria

Reputation: 9185

The best way is to use react-router library. I highly recommend you to follow the tutorial.

You will get something like this after setting up react-router in your app.

  <Router history={hashHistory}>
    <Route path="/" component={App}/>
    <Route path="/students" component={Students}/>
  </Router>

The next step is to use Link component from react-router.

// modules/App.js
import React from 'react'
import { Link } from 'react-router'

export default React.Component {
  render() {
    return (
      <div>
        <h1>Your App</h1>
        <ul role="nav">
          <li><Link to="/about">About</Link></li>
          <li><Link to="/students">Students</Link></li>
        </ul>
      </div>
    )
  }
}

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281686

What you need is not an onClick handler on the button but a Link to route to the correct component

Try and specify route for Student component

and then use

<button type="button"><Link to="/Students">Students</Link>

onClick expects a function and not a component that is why you get an error

Upvotes: 2

Related Questions