1

I am using react-bootstrap Tabs and Tab to build a 3 tab display with basic content. First two tabs will have an input box and a button. After entering some detail in the input box, i want to click on the Save button to go to the next tab.

I am using React state but somehow every-time, i click on the Save button it takes me to the second tab for a second and then since the useState causes a re-render, my component is set to the initial first state instead of navigating and remaining on the second state.

How do i get my component to persist and stay on the second state and then do the same thing there and move on to the last state?

Pasting code as below:

import React, { useState, useEffect } from 'react';
import Tabs from 'react-bootstrap/Tabs';
import Tab from 'react-bootstrap/Tab';


const ThreeTabbedForm = () => {
    const [activeTab, setActiveTab] = useState('shipping');
    const [shipInfo, setShipInfo] = useState('');
    const [payInfo, setPayInfo] = useState('');

    function toNextTab() {
        handleTabChange();
    }

    function handleTabChange() {
        if (activeTab === 'shipping') {
            setActiveTab('payment');
        }
        if (activeTab === 'payment') {
            setActiveTab('order');
        }
    }

    return (
        <Tabs activeKey={activeTab} onSelect={(k) => handleTabChange}>
            <Tab title="Shipping Info" eventKey="shipping" id="shipping-tab">
                <form>
                    <input type="text" value={shipInfo} placeholder="Shipping info" onChange={e => setShipInfo(e.target.value)} required />
                    <button onClick={toNextTab} style={{ display: 'block' }}>Save</button>
                </form>
            </Tab>

            <Tab title="Payment Info" eventKey="payment" id="payment-tab">
                <form>
                    <input type="text" value={payInfo} onChange={e => setPayInfo(e.target.value)} placeholder="Payment info" />
                    <button onClick={toNextTab} style={{ display: 'block' }}>Save</button>
                    <p>Shipping info: {shipInfo}</p>
                </form>
            </Tab>

            <Tab title="Order Info" eventKey="order" id="order-tab">
                <form>
                    <h2>Order placed successfully</h2>
                    <p>Shipping info: {shipInfo}</p>
                    <p>Payment info: {payInfo}</p>
                </form>
            </Tab>
        </Tabs>
    )
}

export default ThreeTabbedForm

4 Answers 4

1

You can use event.preventDefault() to stop the page from reloading like this:

function toNextTab(e) {
  e.preventDefault();
  handleTabChange();
}

// ...

<button onClick={e => toNextTab(e)} style={{ display: 'block' }}>
  Save
</button>

// ... repeat for your other buttons

So basically you don't have to persist these values if the page doesn't reload.

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

2 Comments

Hey that was an unintended piece that snuck in from earlier when i was trying something out. But for confirmation, i removed that and tried again and the problem still persists.
Okay can you edit your question to reflect your current codebase?
0

It is not because of re-rendering because re-rendering does not reset the state. It is because of re-loading. Please use event.preventDefault()

Comments

0
  • add event.preventDefault() like Mr.Bas van der Linden mentioned.
  • don't use defaultActiveKey={something} it's not working
  • the one worked fine is activeKey={activeTab}
  • and your code should work just fine with this small modification

Comments

0

You can set the props mountOnEnter and unmountOnExit as true on the Tab component.

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.