0

so I have the following problem. I have a react page with tabs using Semantic UI tabs (https://react.semantic-ui.com/modules/tab/).

The tabs look like the following: all users | user details

in the 'all users' tab page, a table is created and filled with sql content. It contains a button for every player for more information.

If I click the button, I want:
1. the tab changes to 'user details'
2. the table button has an id, that should be passed to the second tab.

I'm a newbie and doesn't have any code to provide. I checked you can use a component and pass values by doing

Thank you in advance, Doe

1 Answer 1

1

There are two ways of doing it

  1. Store the currentTab, id in the component state. import React, { Component } from 'react'

        const TABS = {
          USERS: 'users',
          USER_DETAILS: 'userDetails'
        }
    
        class Users extends Component {
          state = {
            tab: TABS.USERS
            currentUserId: null
          }
    
          handleUserClick = (id) => {
            this.setState({ tab: TABS.USER_DETAILS, currentUserId: id })
          }
    
          render() {
            const { tab } = this.state
    
            if (tab.USER_DETAILS === TABS.USER_DETAILS && currentUserId) {
              return (
                ...
                  User Details UI
                ...
              )
            }
    
            return (
                ...
                  <Table>
                    ...
                      <td><button onClick={() => this.handleUserClick(id)} /></td>
                    ...
                  </Table>
                ...
              )
          }
        }
    
    1. If you are using react-router and can have a different page for each tab. (different URLs like .../users and .../users/:id for the user details.

      ...
      import { Link } from 'react-router-dom'
      ...
      
      const usersPage = (props) => {
         return (
           <table>
            ...
              <td>
                <Linkto={{
                    pathname: `/users/${id}`,
                  }}
                />
            ...
           </table>
      
         )
       }
      
      // user details page, use can use the useHistory hook from the react-router but for bravity I will just refer to the history
      const userDetailsPage = (props) => {
        ...
        const { match } = props
        const { id } = match.params.id
        ...
      }
      ...
      
Sign up to request clarification or add additional context in comments.

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.