I'm trying to initialize data into form, show the form and navigate to other component (manually). After I successfully load data into form I found myself in a dead end, that I am unable to navigate to another component because I am getting TypeError: path.lastIndexOf is not a function error.
I am using redux-form and component is connected via withRouter.
I tried to workaround it with changing to React.Component, but after I managed to get data into form and connect component with Router, data in the form was unable to change (I was unable to even interact with the form). So I switched back to old solution that gave me that error.
Used versions:
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"redux": "^4.0.1",
"redux-form": "^8.1.0",
"redux-thunk": "^2.3.0",
userProfile.jsx
const renderField = ({
input,
label,
placeholder,
type,
meta: { touched, error },
...rest
}) => (
<div>
<label htmlFor={label}>{label}</label>
<div>
<input {...input} {...rest} type={type} />
{touched
&& ((error && <span className="error-text">{error}</span>))}
</div>
</div>
);
let userProfile = (props) => {
const {
handleSubmit, pristine, submitting, reset,
} = props;
const confirmTitle = 'Are you sure about clearing the changed values?';
const confirmDescription = 'Please notice that you can lose the data you edited';
return (
<div className="container__form" style={{ height: '160vh' }}>
<Confirm title={confirmTitle} description={confirmDescription}>
{ confirm => (
<form className="extended-form" onSubmit={handleSubmit}>
<h3> Personal Data </h3>
<div className="form-control">
<Field
name="username"
type="text"
component={renderField}
label="User name"
disabled
/>
</div>
<div className="form-control">
<Field
name="email"
type="text"
component={renderField}
label="E-mail"
disabled
/>
</div>
<div className="form-control">
<Field
name="user.firstName"
type="text"
component={renderField}
label="First Name"
validate={required()}
/>
</div>
<div className="form-control">
<Field
name="user.lastName"
type="text"
component={renderField}
label="Last Name"
validate={required()}
/>
</div>
<div>
<div className="form-actions">
<button type="submit" disabled={submitting}>
Submit
</button>
{/* </div>
<div className="form-actions"> */}
<button type="button" disabled={pristine || submitting} onClick={confirm(() => reset())}>
Cancel
</button>
</div>
</div>
</form>
)}
</Confirm>
</div>
);
};
export default compose(
withRouter,
connect(
state => ({
...state,
initialValues: state.user.data,
}),
),
reduxForm({
form: 'userProfileForm',
enableReinitialize: true,
}),
)(userProfile);
UserProfileComponent.jsx
const UserProfileComponent = (props) => {
const sendData = (data) => {
props.clearErrors();
props.updateUserData(data);
};
const { errors } = props;
return (
<div className="extended">
<h2> Extended Register process </h2>
<div className="tooltip"> i </div>
<span className="pale-magenta-text"> The mandatory fills are indicated by a * in the right site of the field name </span>
<UserProfile onSubmit={sendData} />
<br />
{errors && (<div className="error-text">{errors.error}</div>)}
</div>
);
};
const mapStateToProps = state => ({
...state,
errors: state.errors,
});
const mapDispatchToProps = dispatch => ({
updateUserData: data => dispatch(updateUserData(data)),
clearErrors: () => dispatch(clearErrors()),
});
export default connect(
mapStateToProps,
mapDispatchToProps,
)(UserProfileComponent);
I expect initialize of the form to be working and also, that I am connected to the Router correctly.
The problem is I think in HOC, because when I change the order of reduxForm and connect, I can navigate but I cannot see any data initialized into form. But that's sadly my dead end I cannot move from. Thanks a lot for any help or suggestion.
I did a lot of debug and changes and finally I found a way how to make it all work.
It's maybe not the best answer, but it can help someone with the same problem:
I removed withRouter (as it was not needed anymore) and changed the order of HOC in UserProfile.jsx to match:
export default compose(
reduxForm({
form: 'userProfileForm',
enableReinitialize: true,
}),
connect(
state => ({
...state,
initialValues: state.user.data,
}),
),
)(userProfile);
I hard coded initialValues in UserProfileComponent.jsx* to match:
<UserProfile onSubmit={sendData} initialValues={props.user.data}/>
Now the data is loaded, user can interact with the form (validation is working) and also, user can fully navigate in the site with no errors.
I hope it helps someone to solve same or similar problem that I had.
Enjoy
Related
I cannot type in the text input of redux form.
it's a very minimal form
function Login({ handleSubmit, handleChange }) {
const [username, setUsername] = useState(undefined);
const [password, setPassword] = useState(undefined);
const onSubmit = (e) => {
console.log(e);
console.log(username);
console.log(password);
};
console.log(handleSubmit);
return (
<Container>
<div className={styles.centered}>
<div className={styles.form}>
<div className={styles.title}>
<H3>Login</H3>
</div>
<form onSubmit={() => handleSubmit(onSubmit)} className={styles.flexColumn}>
<div className={styles.username}>
<P>username</P>
<Field name="username" component="input" type="text" className={styles.input} />
</div>
<div className={styles.password}>
<P>password</P>
<Field name="password" component="input" type="password" className={styles.input} />
</div>
<div className={styles.downSection}>
<Flex>
<P>
Serve Aiuto?
</P>
<a href="#">
<div className={styles.contactLink}>
<P>Contattaci</P>
</div>
</a>
</Flex>
<Button type="submit" text="Accedi" />
</div>
</form>
</div>
</div>
</Container>
);
}
const mapDispatchToProps = {
login: loginAction,
};
const enhance = compose(
connect(null, mapDispatchToProps),
reduxForm({ form: 'login' }),
);
export default enhance(Login);
The handleSubmit doesn't work, i cannot console.log anything.
I tried to see the documentation and tried to search some answer on SO but i didn't find an answer.
Could you please tell me where is the error ? thanks.
So give this a try, let's leave enhance out, I don't know what it does honestly so let's try this type of Login configuration where we turn the component into a class-based one which is good practice anyway since you are receiving inputs from a user.
I do realize you are using useState which is some of the cool new features with React, but what I am recommending is to put together a less complex and conventional setup with a class-based component like so:
import React, { Component } from "react";
import { reduxForm, Field } from "redux-form";
class Login extends Component {
render() {
return (
<form>
<fieldset>
<label>Email</label>
<Field
name="email"
type="text"
component="input"
/>
</fieldset>
<fieldset>
<label>Password</label>
<Field
name="password"
type="password"
component="input"
/>
</fieldset>
</form>
);
}
}
export default reduxForm({ form: "login" })(Login);
Use this to check to see if you can now type into your inputs and then start adding stuff back in and test it every single time until you find the cause of the problem.
Try first just to handle the event
<form onSubmit={onSubmit} className={styles.flexColumn}>
after that try using the this in the function onsubmit and remove the const
onSubmit(event){
console.log(e);
console.log(username);
console.log(password);
this.handleSubmit(event.target.value);
};
after several hours and a special night of bug fixing i discovered the problem:
it was in one import, exactly:
import { Field, reduxForm } from 'redux-form/immutable';
and not
import { Field, reduxForm } from 'redux-form';
this was completely unexpected, i was pretty sure that the mistake was in the body of the component, not in the import.
the structure of the file was ok.
I'm currently creating a form using react-final-form and trying to use react-phone-number-input with it through integration as an adapter, as displayed through this example.
I attempted to use the example to learn how it is done, but I'm not sure how to access the component and create the adapter for it properly.
import React from 'react';
import { Form, Field } from 'react-final-form';
import PhoneInput from 'react-phone-number-input';
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const onSubmit = async values => {
await sleep(300)
window.alert(JSON.stringify(values, 0, 2))
}
const PhoneAdapter = ({ input, meta, ...rest }) => (
<PhoneInput
{...input}
{...rest}
value={input.value}
onChange={(event, value) => input.onChange(value)}
/>
)
class ContactForm extends React.Component {
render() {
return (
<>
<Form
onSubmit={onSubmit}
initialValues={{ }}
render={({ handleSubmit, form, submitting, pristine, values }) => (
<form onSubmit={handleSubmit}>
<fieldset>
<Field component={PhoneAdapter} />
</fieldset>
<fieldset>
<button type="submit" disabled={submitting || pristine}>
Submit
</button>
</fieldset>
<pre>{JSON.stringify(values, 0, 2)}</pre>
</form>
)}
/>
</>
);
}
}
export default ContactForm;
Update: July 2019
Apparently, all you need to do is to spread the input property of Field. Works flawlessly. Learn about spreading if you're not familiar with it.
const PhoneAdapter = ({ input }) => (
<PhoneInput {...input} />
)
<Field name="phone" placeholder="Enter phone number" component={PhoneAdapter} />
I ended up experimenting with the FieldRenderProps props until it worked out. I wasn't so sure whether it would work or not, as react-phone-number-input is two elements in a component. I thought it would implement the input on only one of the elements.
By using input, I gain access to the input's props. Hence, I called upon it's value, as the default looks like so:
<PhoneInput
placeholder="Enter phone number"
value={ this.state.value } // This is what I called.
onChange={ value => this.setState({ value }) }/>
I then did the same for the onChange function prop.
const PhoneAdapter = ({ input }) => (
<PhoneInput value={input.value.value} onChange={value => input.onChange(value)} />
)
Finally, I used the component adapter like so:
<Field name="phone" placeholder="Enter phone number" component={PhoneAdapter} />
In my react component I am trying to set a field called 'total'. I have imported the change action as a prop into my component:
import React, { Component, Fragment } from 'react'
import { Field, FieldArray, reduxForm, getFormValues, change } from 'redux-form'
import { connect } from 'react-redux'
import { CalcTotal } from './calculationHelper';
const renderField = ({ input, label, type, meta: { touched, error } }) => (
<div>
<label>{label}</label>
<div>
<input {...input} type={type} placeholder={label} />
{touched && error && <span>{error}</span>}
</div>
</div>
)
const renderMods = ({ fields, meta: { error, submitFailed } }) => (
<Fragment>
<ul>
<li>
<button type="button" onClick={() => fields.push({})}>
Add Modification
</button>
{submitFailed && error && <span>{error}</span>}
</li>
{fields.map((mod, index) => (
<li key={index}>
<button
type="button"
title="Remove Mod"
onClick={() => fields.remove(index)}
/>
<h4>Mod #{index + 1}</h4>
<Field
name={`${mod}.lastYear`}
type="number"
component={renderField}
label="Last Year"
/>
<Field
name={`${mod}.currentYear`}
type="number"
component={renderField}
label="Current Year"
/>
<Field name={`${mod}.type`} component="select" label="Type">
<option />
<option value="-">Expense</option>
<option value="+">Income</option>
<option value="-">Tax</option>
</Field>
</li>
))}
</ul>
<Field
name="total"
type="number"
component="input"
label="Total modifications"
text="0"
/>
</Fragment>
)
class FieldArraysForm extends Component {
render() {
const { handleSubmit, formValues, change } = this.props
if (formValues) {
console.log('formvalues', formValues);
const test = CalcTotal(2000);
console.log('calc=', test);
debugger
this.props.change('fieldArraysForm', 'total', 5000)
}
return (
<form onSubmit={handleSubmit}>
{/* <button onClick={this.changeStuff}>set total</button> */}
<FieldArray name="mods" component={renderMods} />
<div>
<button type="submit" >
Submit
</button>
</div>
</form>
)
}
}
const mapStateToProps = (state) => ({
formValues: getFormValues('fieldArraysForm')(state),
});
const mapDispatchToProps = {
change
};
// const Example = reduxForm({
// form: 'fieldArraysForm', // a unique identifier for this form
// })(FieldArraysForm)
// const ConnectedForm = connect(
// mapStateToProps,
// mapDispatchToProps,
// )(Example);
// export default ConnectedForm
export default reduxForm({
form: "fieldArraysForm"
})(
connect(
mapStateToProps,
mapDispatchToProps
)(FieldArraysForm)
);
The line where the code fall into an infinite loop:
this.props.change('fieldArraysForm', 'total', 5000)
How /where do I put this statement to make sure the 'total' field is changed and not get into a loop?Which React lifecycle event would suit? I want to fire this whenever there is a form change on any field.
You'll need to move your statement out of the render method and into the componentDidUpdate lifecycle method (you also need an if statement to prevent an infinite loop):
componentDidUpdate(prevProps) {
if (this.props.someValue !== prevProps.someValue) {
this.props.change("formName", "formField", "newFormValue");
}
}
Working example: https://codesandbox.io/s/r5zz36lqnn (selecting the Has Email? radio button populates the email field with test#example.com, unselecting the radio button resets the email field to "")
Probably I've checked all the docs and questions out there about this problem but I still couldn't fix it since long time. So, I decided to ask here.
As the title says I am trying to submit a redux form from its parent component. I tried adding hidden submit input, into the form and that works although this isn't the redux form way. What should I do?
Thanks for your help.
WorkExperienceForm.js
import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { renderHorizontalTextField } from '../Fields/TextFields';
import { renderTextAreaFieldWithLabelAndPopover } from '../Fields/TextAreaFields';
const WorkExperienceForm = ({ handleSubmit, onSubmit, shouldHide, form }) => {
if(shouldHide) return false;
return(
<form onSubmit={ handleSubmit(onSubmit) } className="form-horizontal">
<Field name="companyName"
type="text"
label="Company Name"
placeholder="Apple inc."
id="input-company-name"
component={renderHorizontalTextField} />
<Field name="title"
type="text"
label="Title"
placeholder="Marketing Specialist"
id="input-title"
component={renderHorizontalTextField} />
<Field name="startYear"
type="text"
label="Start Year"
placeholder=""
id="input-start-year"
component={renderHorizontalTextField} />
<Field name="endYear"
type="text"
label="End Year"
placeholder="Blank if current"
id="input-end-year"
component={renderHorizontalTextField} />
<Field name="summary"
rows="4"
label="Summary"
placeholder="Summary..."
id="input-summary"
component={renderTextAreaFieldWithLabelAndPopover} />
</form>
)
}
export default reduxForm({
enableReinitialize: true
})(WorkExperienceForm);
This is the parent component
onSubmit(values){
this.props.createWorkExperience(values, () => {
this.props.notify();
})
}
render(){
if(!this.props.candidate || !this.props.candidate['workExperience'].length > 0) return this.renderEmptyState();
const activeClass = this.state.displayForm ? 'btn btn-success btn-block mt8' : 'btn btn-primary btn-block mt8'
return(
<div>
{ this.renderWorkExperience() }
<WorkExperienceForm {...this.props}
form='postWorkExperienceForm'
onSubmit={this.onSubmit}
shouldHide={!this.state.displayForm} />
<button type={ this.state.displayForm ? 'submit' : 'button' }
htmlFor='postWorkExperienceForm'
className={activeClass} >{ this.state.displayForm ? 'Save' : 'Add Work Experience' }
</button>
</div>
)
}
Yeah, the documentation is sparse regarding this matter.
I think you may have Context issues. I think you should move the button to the Form Component and pass the mySubmit (renamed it from onSubmit for clarity) function to the Form Component with an arrow function via onSubmit property.
mySubmit(formValues){
this.props.createWorkExperience(formValues, () => {
this.props.notify();
})
}
<WorkExperienceForm {...this.props}
form='postWorkExperienceForm'
onSubmit={(formValues) => this.mySubmit(formValues)}
shouldHide={!this.state.displayForm} />
Then in the Form Component wrap the onSubmit in the handleSubmit function.
<form onSubmit={handleSubmit(onSubmit)}>
Here is a example where we recently had to do this with Redux-Form.
Maybe, it will help you some.
Note, we are using FLOW.
Parent Component: beer.add.component.js
Form Component: beer.add.form.component.js
Live production example, so you know that it works.
Note, The SignInForm uses the same pattern.
I am using the Redux-form to do a task.
This form in a form container.
In the form container or in the form component.
There are two buttons. An add button and a subtract button.
The form component is:
import React from 'react'
import { Field, reduxForm } from 'redux-form'
import TextField from 'material-ui/TextField'
import RaisedButton from 'material-ui/RaisedButton'
const renderTextField = ({ input, label, meta: { touched, error }, ...custom }) => (
<TextField hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
)
const ActivityDetailForm = props => {
const { handleSubmit, pristine, reset, submitting,} = props
return (
<form onSubmit={handleSubmit}>
<div>
<RaisedButton
type="submit"
disabled={pristine || submitting}
label="saveChange"
fullWidth={true}
secondary={true}
/>
</div>
</form>
)
}
export default reduxForm({
form: 'ActivityDetailForm', // a unique identifier for this form
})(ActivityDetailForm)
Now, I face a problem. When I click the add button,
<div>
<Field name="field1" component={renderTextField} label="text1: "/>
</div>
the code above will be created in the form element.
When I click the add button again, the div element which includes the Field named field2 will be created in the form element.
... Field named field3
... Field named field4
... Field named field5
...
When I click the subtract button. The last Field element will be destroyed.
Do you know the method to solve this problem?
The following (untested) is a pretty basic example on how to achieve dynamic inputs with a FieldArray. You'd have to tweak this a bit to tailor it to your specific scenario.
const renderTextField = ({ input, label, meta: { touched, error }, ...custom }) => (
<TextField hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
)
const ActivityDetailForm = props => {
const { handleSubmit, pristine, reset, submitting,} = props
const renderFieldArray = ({ fields }) => (
<div>
<div>
<RaisedButton
onTouchTap={() => fields.push({})}
label="Add"
/>
</div>
{fields.map((field, index) => (
<div>
<div key={index}>
<Field
name={`${field}.name`}
label={`Text ${index + 1}`}
component={renderTextField}
/>
</div>
<div>
<RaisedButton
onTouchTap={() => fields.remove(index)}
label="Remove"
/>
</div>
</div>
))}
</div>
);
return (
<form onSubmit={handleSubmit}>
<div>
<FieldArray
name="textFields"
component={renderFieldArray}
/>
<RaisedButton
type="submit"
disabled={pristine || submitting}
label="saveChange"
fullWidth={true}
secondary={true}
/>
</div>
</form>
)
}
export default reduxForm({
form: 'ActivityDetailForm', // a unique identifier for this form
})(ActivityDetailForm)