I'm new to reactjs, i wanted to post the message through the fetch method successfully, as i 've raised the query but the solution didn't worked out for me and given negative votes for the query. I'm writing my problem because i have gone through similar links and i have tried but not getting expected output. Sometimes it's working fine and the next day it is showing error in res(json) line. And want to clear the message after posting but when i use this link solution "How to clear the input after posting the data through fetch and making the code execute successful" sometimes the message is getting disappear and sometimes message is not getting posted. Anyone help me in this? thanks in advance.
Asked
Active
Viewed 54 times
0
-
Where exactly are you getting the problem ? Have you tried checking the network tab in your browser to see if any http error codes are showing up ? – Muljayan Nov 27 '19 at 06:30
-
@Muljayan - 1st issue - I'm unable to clear the message which i've sent. 2nd issue - if it is working then i couldn't post the messages. Mainly if 1st issue get solved then in chrome it is showing ".then(res => res.json())" as an error – Chandan Nov 27 '19 at 06:38
-
@Muljayan - While sending message in chrome it is showing like this as an error "Unhandled Rejection (SyntaxError): Unexpected end of JSON input" – Chandan Nov 27 '19 at 06:45
-
I'm not even sure res.json() here is a function or method provided by the response from the fetch, which is probably throwing an error. You should call a `.catch` to catch the error and log it. – andytham Nov 27 '19 at 06:51
-
@andytham - can you see, in the first component, there is a post method after that there is ".then(res => res.json())" i have written. that is the point i'm getting error. If i have to write `.catch` then could you please tell me what to write in it. i'm not sure what i should in `.catch` – Chandan Nov 27 '19 at 07:03
-
Put it at the end of your promise chain like this ```.then(() => { this.setState({ body: "" }) }).catch( err => {console.log(err)}) ``` – andytham Nov 27 '19 at 07:14
-
@andytham - Thanks i'm not getting error but raised the same issue unable to clear message after posting it – Chandan Nov 27 '19 at 07:19
1 Answers
0
You need to use the value from the state inside input to be able to clear it:
<input
className="sendMessage"
onChange={this.handleChange}
name="body"
value={this.state.body} // Add input value
/>

Clarity
- 10,730
- 2
- 25
- 35
-
-
I'll try to write this in another way... will go through some more links thanks! – Chandan Nov 28 '19 at 04:08