title: How create separate component for prev/next buttons ReactJS
body: <p>I created a simple application using ReactJS. This app is the profiles list, where for simplicity when displaying a profile I only show the name.
  I added buttons to display the previous and next profiles, but I did it very simple way and inside the main component.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>how to separate this buttons from a separate component</li>
  <li>how I can improve the "toggle" functions for these buttons </li>
  </ol>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// my profiles.json
  let profiles = [{"name":"John"}, {"name":"Kitty"}, {"name":"Ji"}, {"name":"Mattis"}]
  
  class Profile extends React.Component {
    render() {
      return (
        &lt;div&gt;
           &lt;h1&gt;{this.props.name}&lt;/h1&gt;
        &lt;/div&gt;
      )
    }
  }
  
  class Main extends React.Component {
    constructor(props) {
      super(props)
  
      this.state = {
        index: 1,
        disabledNext: false,
        disabledPrev: false
      }
    }
    togglePrev(e) {
      let index = this.state.index - 1
      let disabledPrev = false
      if (index &lt;= 0) {
        e.preventDefault()
        index = 0
        disabledPrev = true
      }
  
      this.setState({ index: index, disabledPrev: disabledPrev, disabledNext: false })
    }
  
    toggleNext(e) {
      let index = this.state.index + 1
      let disabledNext = false
      if (index === this.props.profiles.length - 1) {
        e.preventDefault()
        index = this.props.profiles.length - 1
        disabledNext = true
      }
  
      this.setState({ index: index, disabledNext: disabledNext, disabledPrev: false })
    }
    render() {
      const { index, disabledNext, disabledPrev } = this.state
      const profile = this.props.profiles ? this.props.profiles[index] : null
      if (profile) {
        return (
         &lt;div className='profile'&gt;
            &lt;div&gt;
              &lt;button onClick={this.togglePrev.bind(this)} disabled={disabledPrev}&gt;Prev&lt;/button&gt;
              &lt;button onClick={this.toggleNext.bind(this)} disabled={disabledNext}&gt;Next&lt;/button&gt;
            &lt;/div&gt;
            &lt;Profile {...profile} /&gt;
         &lt;/div&gt;
        )
      } else {
        return &lt;span&gt;error&lt;/span&gt;
      }
    }
  }
  
  const App = () =&gt; (
    &lt;div&gt;
      &lt;Main profiles={profiles} /&gt;
    &lt;/div&gt;
  )
  
  ReactDOM.render(
    &lt;App /&gt;,
    document.getElementById('root')
  )</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"&gt;&lt;/script&gt;
  &lt;script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"&gt;&lt;/script&gt;
  &lt;div id="root"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>