Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Dropdown: ensure reactivity of components 'selectedItem' prop #1111

Merged
merged 9 commits into from
Jun 25, 2024

Conversation

skykanin
Copy link
Contributor

Bug: Changing the selectedItem prop doesn't rerender the component because it's immediately being captures inside a useState hook.

Fix: Add a useEffect hook that runs the setState hooks where selectedItem is being used to initialise the state hooks.

Testing: I tested this by adding a simple component to the storybook and observed that clicking the button to change the selected item works with my fix.

export const SelectItemTest = () => {
	const [select, setSelect] = React.useState();
	return (
		<div>
			<Button onClick={() => setSelect({ title: "Ocean", id: "item3" })}>
				Select Ocean
			</Button>
			<Dropdown header="Menu header" selectedItem={select} items={items} />
		</div>
	);
};

SelectItemTest.story = {
	name: "SelectItem test",
};

@skykanin skykanin changed the title [Bugfix] Dropdown: ensure reactivity of Dropdown components 'selectedItem' prop [Bugfix] Dropdown: ensure reactivity of components 'selectedItem' prop Jun 13, 2024
@johnnadeluy
Copy link
Contributor

The build is failing on linting;npm run lint:fix should fix it 😊

@annesiri annesiri assigned johnnadeluy and unassigned annesiri Jun 21, 2024
@@ -39,6 +39,8 @@ const Input = forwardRef(
handleChange(e.target.value)
}

useEffect(() => setValue(value), [value])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be implemented in a separate PR; we are looking into making this component stateless

Copy link

sonarcloud bot commented Jun 25, 2024

@skykanin skykanin merged commit bd67e9e into master Jun 25, 2024
6 checks passed
@skykanin skykanin deleted the bugfix/dropdown-component-reactivity branch June 25, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants