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