0

I want a table of data to refresh on button click after the POST request has been submitted.

I have a react button:

<Button onClick={this.click} className="domain-button" type='primary'>Add</Button>

And the corresponding click function, and refreshPage function:

async click() {
  const { domainInputValue } = this.state;
  console.log( domainInputValue );

  const response = await
  fetch('/new-cert', {
      headers: {
          'Content-Type': 'application/json'
      },

      method: 'POST',
      body: JSON.stringify({domainInput: domainInputValue})
  });

  const body = await response.text()
  console.log(body)

  if (response.status !== 200) throw Error(body.message);
  return body;
}

refreshPage() {
  window.location.reload();
}

And in my back-end Nodejs to handle the POST request I have:

app.post('/new-cert', function (req, res) {
    fs.appendFile('certs-list', '\n' + req.body.domainInput, function (err) {
        if (err) throw err;
    });
    res.send('POST request: ');

    console.log("INSERTING")
    exec('sh cert-check-script-insert.sh');

    console.log(req.body);
    console.log('post is working!')
});

I'm not sure where to call refreshPage(), I've tried calling it right after click() but this seems to be too early and the refresh does not change the data displayed in the table.

2
  • You could have a posted variable in state, which changes after the POST. This will cause the component to re-render, which seems to be what you want. Commented Apr 17, 2018 at 15:57
  • @Colin thanks for the suggestion, could you possibly write an answer please, just not sure where to put things Commented Apr 17, 2018 at 16:06

3 Answers 3

0

Delay the server response until the server is ready for the reload - in your case, looks like you should do res.send and maybe also exec inside the callback to fs.appendFile. Then your fetch will not resolve until the server is definitely ready, and you do your reload then.

Refreshing the whole page to get new data is not very React, but that's none of my business.

Sign up to request clarification or add additional context in comments.

5 Comments

Should I just be re-rendering the component? And if so could you suggest how to, sorry I'm new to this
Yeah, something like that. Where does the new data come from when you reload?
Maybe you could send the new data back as the response from the POST request and solve both problems at once.
It's coming from a back-end API with path /list-certs that comes from a local file called certs.json. I've tried to add a fs.readFile call to certs.json after the fs.appendFile call to certs-list if that's what you mean?
So your component is doing another fetch to get the data? You should be able to make it do that again instead of refreshing everything.
0

Put the refreshPage() call after await response.text():

async click() {
  const { domainInputValue } = this.state;
  console.log( domainInputValue );

  const response = await
  fetch('/new-cert', {
      headers: {
          'Content-Type': 'application/json'
      },

      method: 'POST',
      body: JSON.stringify({domainInput: domainInputValue})
  });

  const body = await response.text()

  refreshPage();

  console.log(body)

  if (response.status !== 200) throw Error(body.message);
  return body;
}

I agree with @ben-west, though -- I'd recommend using state in React to handle this. Essentially, you'll want to setState with whatever data you care about from the response, and then use that data from this.state wherever you need it in your component.

1 Comment

Thanks for the answer but it seems to be a premature reload as it doesn't display the new data and I have to reload again for it to appear. Could you possibly write an answer using States please? I'm new to React and Nodejs and still trying to get my head around it
0

I ended up adding a function componentDidUpdate() which mimics my componentDidMount() behaviour:

componentDidMount() {
    this.callApi()
        .then(res => {
            this.setState({
                json: res.map(x => x),
            })
        })
        .catch(err => console.log(err));
}


componentDidUpdate() {
    this.callApi()
        .then(res => {
            this.setState({
                json: res.map(x => x),
            })
        })
        .catch(err => console.log(err));
}

1 Comment

If you're duplicating code like this, I'd recommend pulling this out into a function and calling it from componentDidUpdate and componentDidMount.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.