J'utilise map pour effectuer une boucle sur un tableau d'objets. J'utilise ces données pour remplir un formulaire mais j'ai des problèmes avec la fonction handleInputChange
. Comment initier handleInputChange
lorsque j'utilise un composant. L'erreur que je reçois est this.setState is not a function at handleInputChange
Erreurs de formulaire avec handleInputChange dans le composant
Chemin: formComponent.jsx
return (
<li>
<SingleInput
inputType={'text'}
title={'Company name'}
name={'position.company'}
controlFunc={this.props.handleInputChange}
content={this.props.company}
placeholder={'Company name'}
bsSize={null}
/>
</li>
);
CareerHistoryPositionsUpdateForm.propTypes = {
company: PropTypes.string,
title: PropTypes.string,
controlFunc: PropTypes.string
};
chemin: `form.jsx »
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
renderPositions() {
const profileCandidateCollection = this.props.profileCandidate;
const careerHistoryPositions = profileCandidateCollection && profileCandidateCollection.careerHistoryPositions;
if (careerHistoryPositions) {
return careerHistoryPositions.map((position) => {
return (
<CareerHistoryPositionsUpdateForm
key={position.uniqueId}
company={position.company}
controlFunc={this.handleInputChange}
/>
)
})
}
}
render() {
return (
<form className="careerHistoryForm" onSubmit={this.handleFormSubmit}>
<ul>
<p>Test</p>
{this.renderPositions()}
</ul>
<input type="submit" className="btn btn-primary" value="Save" />
</form>
);
}
Copie possible de [Impossible d'accéder à l'instance React (this) dans le gestionnaire d'événements] (https://stackoverflow.com/questions/29577977/unable-to-access-react-instance-this-inside-event-handler) –