how to clear selected checkboxes programatically from parent component - reactjs

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)

Related

Formik handling state with array of objects

I'm newly using Formik library for forms and I ran into problem. I'm not sure how can I handle state with array of objects from inside the component. I know about setValues and setFieldValue methods.. also about FieldArray but my target is to add newZone into newZones array when I click on add button and I don't want to render it. I want to customize rendering and place it under accordion.
sandbox here: https://codesandbox.io/s/rough-haze-zdlw9?file=/src/App.js
Will I need all custom handlers? Will I need to use local state inside component and then use setFieldValue to set state? Or can I somehow use FieldArray in there without rendering?
Thank you
You are using React-Bootstrap components instead Formik components, so you must manually assign handleChange and value props to every input component. You also have to call setFieldValue() when user clicks button Add.
Example:
https://codesandbox.io/s/quirky-leaf-tr3rv

set disabled on a form item through form ref

I am currently using antd-form-builder to build multiple JSON forms for different components and I have an issue with antd trying to use a checkbox to disable another input.. basically I have a function that returns the form meta fields and then in the onChange for the checkbox I am trying to set another field to become disabled
what I have managed to find so far is for the form ref there is a setFields function and I can use this to change the value of another field or the errors on that field through the onChange of the checkbox, however, I cannot change the disabled state of another field.
turns out, this cant be done .. and state variables should be used as mentioned in the issue here

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.

How to pass filter-params with React?

I'm using Semantic-React and I have component-filter which consists of checkbox groups. Every checkbox has id and I must pass them in order to filter the results.First idea was to perform method which would be called after checkbox onCLick(which would pass checkbox id). And in this method set in state an object and after every checkbox click change it. I remember, that in Jquery exists $('form').serialize() which everytime checks form elements and gets checked values automaticaly. Exists something like this in react, or semantic-ui react? I would be simplier to use such method than to create and control object in state
You should use "controlled components":
Filter component should keep a set of checked ids as part of state.
Checkbox checked property should be set to true if the set contains box id (and to false otherwise) in Filter component render.
Checkbox onChange should be handled by filter component and modify state.
Filter can access it's state handling checkbox onChange or button "Apply" onClick (or any other event handled by Filter component).
Be careful: setState is an async function and directly reading other component state can lead to data race, so state must be read after proper event fired by React. Also there are a bunch of state-keepers like Redux.
It is described in official docs here: https://reactjs.org/docs/forms.html

Get a select field to show other select fields based on id of selection

I am working with material-ui and React. I have a SelectField that is part of a component which is a grandchild of the parent component. What I need is to take the value of the item in the SelectField and then use that to display information in another SelectField which is a child of this component so a great grandchild of the parent component if you will.
I need to use the value I get from the first SelectField to make an API request to get the info that I need to show in the next SelectField.
I can successfully get the value from the first SelectField but I am struggling to figure how I can get it to show in the child component.
If you could help me out I would appreciate it.
Thanks for your time
Put the value of your two fields in a state and fill your select fields with thoses values. Create a callback that will call this.setState() to modify the value of the second field when the first one is modified. Attach this callback on yout first field with the property onChange. As a result, the second select field will have a new value. you have an example here
To fix this, I created an array and when I select the value from the first SelectField I updated the state of the array with the values that I wanted to populate the next SelectField with and passed it back from the parent component as props.

Resources