Not able to get state of component - reactjs

I have a component with a checkbox and text field when the user clicks the checkbox I am showing the text field which is hidden by default. To achieve this functionality I am using state of this component, as user checks the checkbox I am calling onChangeHandler and setting the state to showAccountNumberField text field and when user unchecks the checkbox hiding the text field. But when I am trying to test this component by doing
wrapper.setSatate({showAccountNumberField: true})
I am getting error that state can only be accessed on class component. I am using React 16 and Jest for unit testing. Any help is appreciated.

Related

How to make the submit button conditional with react hook form

I am using react hook form accepting various fields, I want to enable the button only when
A date is selected from the controller with KeyboardDatePicker component
A radio button selection is made
An autocomplete selection should select a desired value. If the value selected in not the one that we desire the button should be disabled.
How can I achieve this use case. I tried using isDirty, dirtyFields, isValid, touched and they were not helpful as both of them do not provide the value of the ids controlled using react hook form. Also I need to get the values of the radio button but with these props it is returned true, irrespective of the value selected.

Tabbed Form in Ant Design Issue

Tabbed Form doesn't seem possible in AntDesign. all field error comes when we submit form inside tab
Blockers
On Submit only current active tab form values comes
Tabs are inside the form
all field error Comes when we submit form inside tab
destroyInactiveTabPane i try this but not working
If i have to generate tabs dynamically like English, French,
Italian, i cannot initialling Form as tab loop will come
inside of component
Forced render of content in tabs, not lazy render after
clicking on tabs i try this but not working
Error Validation Trigger when i submit form of all filed with in the form
Create forms inside each tab and in your parent file which will hold the tabs data, active tab manage the state.
Create form for each tab.
Suppose i have name and member tab i will create the form with
Const nameForm = useForm()
Const memberForm =useForm()
And pass each form as props to the component.
Nameform to name component and so on.
And whenever you will try to change the tab or click submit check in function which tab is active and validate that form only.

how to clear selected checkboxes programatically from parent component

I have used the following component in react,
https://www.npmjs.com/package/react-multiselect-checkboxes
I want to clear selected checkboxes because I have to recreate it with new options from another dropdown change event.
So is there any way from where I can clear selected checkboxes from parent component when another dropdown is changed
This is for react
The author of the libarary suggest that its behavior is based on react-select
Have you tried to use value prop?
see https://github.com/JedWatson/react-select#controllable-props
Additionally if you want to manipulate values in parent component you should use onChange callback then and store it somewhere (I assume state will be best place)

React checkbox component: Where should I keep state changes?

In react-table, I've made a custom drop-down-menu component that appears when user clicks on a header of a column.
When user clicks on the option "Choose columns", a modal appears with checkboxes options where user can select which columns to show or hide.
This modal with the checkboxes options is in the drop-down-menu component. The problem is I can not figure out which is the best way to handle state changes. Should I keep state changes on both components (table component and drop-down-menu component)? Should I use redux for that? I'm going to use many tables, so the total number of columns will be very big. I'm really confused about all this.
You should have one source of truth. As the table will need this information, it should be saved in the table and passed to the drop-down-menu component.
Checkout this codesandbox example.
Well if you want to make your checkbox reusable component, which you should, then you will have to keep the state in your checkbox component and expect an onChange event handler from wherever you want to use that checkbox component.

Reusing react component duplicates DOM elements

I have created a React component which opens a form for input parameters and once the form is submitted, a call is made to server and the resulting json is displayed in the UI. This component takes input parameters from dropdown, text box and so on.
So I created a generic component as there are many such instances. I passed the required input through props and displayed the values in the component dynamically.
When I called the second component's instance passing the values, I see that opening the dropdown in the first instance, opens the dropdown from second instance.
Please suggest an approach to make each component's instance unique.
Thanks in advance!

Resources