1

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

;

1
  • 3
    At leasts take the time to correctly format your code, it's a mess. Commented Jan 25, 2017 at 9:47

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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>
    )
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.