score:0
You should use a function instead of an object to set new state.
Cause of update of states is asynchronous and the value of state when you want change the state is not reliable.
Change the add
method like this:
add = targetKey => {
let { activeKey } = this.state;
if (targetKey !== activeKey){
this.setState(prevState => {
const newState = {...prevState};
newState.panes.push({title:'Tab 3', content:'Content of tab pane 3',key: activeKey+1});
return newState;
});
}
};
Also change the remove method like this:
remove = targetKey => {
let { activeKey } = this.state;
let lastIndex;
this.setState(prevState => {
const newState = {...prevState};
newState.forEach((pane, i) => {
if (pane.key === targetKey) {
lastIndex = i - 1;
}
});
const panes = newState.filter(pane => pane.key !== targetKey);
if (panes.length && activeKey === targetKey) {
if (lastIndex >= 0) {
activeKey = panes[lastIndex].key;
} else {
activeKey = panes[0].key;
}
}
return ({panes, activeKey});
});
};
check this link: https://jsfiddle.net/w71yLn24/
Source: stackoverflow.com
Related Query
- how to add a tab dynamically when we click a particular button using react js?
- How to add a tab dynamically when we click a button using React js and Bootsrap 4
- how to add input field dynamically when user click on button in react.js
- how to change background color when I click the button using react hooks
- How to scroll to newly added row in a table automatically when we click on add row button in React JS
- How to show a component When I click a button using function components in react
- How to increment the old array list key 5 times dynamically when I press the button using React Js?
- How to hide a button when I click the button in react using functional components
- How to add styles to individual elements in react using e.target, when a button is clicked?
- How do I add a transition to my navbar menu in react when I click the toggle button
- How to change button background color when I click the button in React using Hooks
- How to Reset a form When I click submit Button I React using React hooks
- How to Add Smooth Scrolling Back To Top Button using react js?
- How can I find and click a button that has no text using React Testing Library?
- how to add row to a table using ReactJS on button click
- How do I add a custom font to my Card Element when using React Stripe Elements?
- How to add floating action button on the right-bottom side of the screen using material ui in react
- How to hide a button when printing using react and styled components?
- How to add new props dynamically to the component using navigator in React Native
- How to dynamically create button for calling specific action using map array in react
- How to make one table record editable on button click in React when the table record is mapped?
- How to get the time in seconds after click on submit button using React Count Down Timer
- In React, how to add class to button when click
- React Js how to jump whole pages to other pages when click on the navigation button
- How to scroll to the particular div on button click in a single page application using react-router?
- Dynamically add errors when using useFieldArray with React Hook Form
- How to change the state of the particular component when it is clicked, using useState in react js?
- How to click a button using React Developer Tools?
- How to restrict formik validation when you click cancel button in react
- How to loop a component when I click a h3 tag in react using Hooks
More Query from same tag
- React/NextJS: Loading an external script only for non-authenticated users
- How to disable redirecting for some elements in an anchor tag?
- React.js Not Rendering?
- Log minified JavaScript errors in production code
- Prevent form from resetting inputs states in ReactJs
- TypeScript + React: Using one prop value to dynamically type another
- How to configure webpack-bundle-analyzer for react?
- HttpOnly cookie not being sent to api
- How to add overlay to material UI table row?
- Why is the routing not working in react.js?
- Bootstrap Modal is not working in React JS
- Can I customize Carousel-indicators in react-responsive-carousel
- how to attach custom event handlers in react?
- Execute two functions in return render react
- sort immutable js data based on the two properties
- How to click on a div in a chessboard using componenents in react?
- React & Typescript: 'JSX' is not defined no-undef
- Get the size of a react component?
- React - pass props/state to rendered HTML string?
- Does React have an equivalent for Angular's DatePipe?
- Images not showing in React?
- integrate firestore from v8 to v9
- How to implement react-dnd useDragLayer?
- Javascript Onclick=window.open() function will open a new page every time I refresh the page?
- Using certain props to trigger a useEffect() React hook, but referencing others in the effect
- silent renew error Frame window timed out on refresh token
- How to reset the timer using UseEffect React Hooks
- Pass function by props in react router
- Material ui custom theme implementation
- Field error while using redux form in React js