I'm trying to post multiple values for tag inside an array of my fetch.
However, it is posting them connected in one array.
I would like it to always post it
how tag currently sent
tag:[{name: [null, null]}]
how tag should be sent
tag:[
{name: "value from field"},
{name: "next value from field"}
]
I also welcome suggestions on a more clean/efficient way to do this form.
here is my code:
class AddNew extends React.Component {
constructor(props) {
super(props);
this.onTitleChange = this.onTitleChange.bind(this);
this.onTagChange = this.onTagChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
options: []
};
}
componentDidMount() {
fetch(TAG_API, {
})
.then(response => response.json())
.then(json => {
this.setState({ options: json });
});
}
onTitleChange(e) {
this.setState({ [e.target.name]: e.target.value });
console.log("the title has changed" + e);
}
onTagChange(value) {
this.setState({ value: value });
console.log("they look like this" + value);
}
handleSubmit(e, value) {
e.preventDefault();
return (
fetch(CREATE_API, {
method: "POST",
body: JSON.stringify({
title: this.state.itemtitle,
tag: [
{
name: this.state.value.map(e => {
e.name;
})
}
]
})
})
.then(res => res.json())
.catch(err => console.error(err))
);
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<textarea
name="itemtitle"
type="text"
placeholder="Add new..."
onChange={this.onTitleChange}
/>
<button type="submit">Save</button>
</form>
<Select
mode="tags"
name="tagfield"
onChange={this.onTagChange}
>
{this.state.options.map(e => (
<Option value={e.name}> {e.name} ({e.taglevel}) </Option>
))}
</Select>
</div>
);
}
}