Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrew: you probably don't need to set state to props #35

Open
eric-do opened this issue Nov 12, 2021 · 0 comments
Open

Andrew: you probably don't need to set state to props #35

eric-do opened this issue Nov 12, 2021 · 0 comments

Comments

@eric-do
Copy link

eric-do commented Nov 12, 2021

When the props change, the component will automatically re-render, so setting state to props is not typically necessary.

Also, if you do need to do this (or something similar), and every state is related to an object, then consider just doing a single set state, i.e. instead of

const setParams = () => {
    setId(props.review.review_id);
    setStars(props.review.rating);
    setDate(props.review.date);
    setSummary(props.review.summary);
    setPhotos(props.review.photos);
    setReco(props.review.recommend);
    setName(props.review.reviewer_name);
    setResponse(props.review.response);
    setHelpful(props.review.helpfulness);
    setRevBod(props.review.body);
  }

You can do something like

const [review, setReview] = useState({
  id: 0,
  stars: 5, 
  ...
});

const setParams = () => {
    setReview({
      id: props.review.review_id,
      stars: preps.review.rating
    })
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant