react-native-clean-form icon indicating copy to clipboard operation
react-native-clean-form copied to clipboard

<Switch value={true} not working

Open thienpow opened this issue 7 years ago • 7 comments

setting the Switch value to true seems not working...

thienpow avatar May 30 '17 16:05 thienpow

how to initialize the switch value?

thienpow avatar Jun 01 '17 05:06 thienpow

@thienpow I haven't used it but looks like you have to set the value prop of Select like <Select value="your value">... You can go through the code at https://github.com/esbenp/react-native-clean-form/blob/master/src/Select.js , look for props and the render function

rascala avatar Jun 02 '17 11:06 rascala

Switch value is not working even though the code base did inherit all props from the base switch component... It only works by initializing the redux Form data... Which you can't really do some dynamic UI event handling that change the switch on other components interaction...

thienpow avatar Jun 03 '17 16:06 thienpow

@thienpow In the example https://github.com/esbenp/react-native-clean-form/blob/master/example/src/Form.js, if I change the value to 'US' or 'DE', then the correct label is pre-selected for me

<FormGroup>
  <Select
	name="country"
	label="Country"
	options={countryOptions}
	// placeholder="Denmark" -- commented this
	value="US"  // -- added this, shows "United State"
/>
</FormGroup>

I am not using redux

rascala avatar Jun 05 '17 22:06 rascala

yeah select is working... i am talking about the <Switch button

thienpow avatar Jun 06 '17 02:06 thienpow

Damn.. Sorry! 🤣 Switch works for me.. Maybe post your code?

rascala avatar Jun 06 '17 06:06 rascala

@thienpow did you add initial data model to state?

I was having the same confusion how to create model driven form.

Followed this https://codesandbox.io/s/MQnD536Km to get the understanding how it gets done in redux-form.

In short you have to connect 'initialValues' to the state and form gets magically filled!

In my case (using https://github.com/reactjs/reselect):

const mapStateToProps = createStructuredSelector({
  initialValues: makeSelectCardFieldValues(),//to init values you're looking for
  fieldSets: makeSelectFieldsets()// to 'draw' form with fieldsets
});

I can confirm the approach is working fine for <Input />, so one could assume it's also working for <Switch />.

reposandermets avatar Jun 23 '17 12:06 reposandermets