Quaquaro / capstone-project

This is my capstone-project, which I created in connection with the webdevelopment-bootcamp at neue fische. TABULA RASA is aimed at all passionate board players who want to keep track of the games they have played. The app allows you to track the results of up to eight players and make notes for the next round.
https://capstone-project-quaquaro.vercel.app/
MIT License
1 stars 0 forks source link

Refactor FormComponent #22

Open Quaquaro opened 2 years ago

Quaquaro commented 2 years ago

Value proposition

As a developer I need to have clean code for my forms to be able to maintain, test and understand the code more easily

Acceptance Criteria

Tasks

Quaquaro commented 2 years ago
const useFetch = (input, { auto, ...init }) => {
  const [result, setResult] = useState([null, null, true]);

  const fetcher = useCallback(
    (query, config) =>
      fetch(query, config)
        .then((res) => res.json())
        .then((data) => setResult([null, data, false]))
        .catch((err) => setResult([err, null, false])),
    [input, init]
  );

  useEffect(() => {
    if (auto) fetcher(input, init);
  }, []); // if you want to fetch data only once, do this.

  return [...result, fetcher];
  //fetcher(refetch) function or can be used for post api call
};
Quaquaro commented 2 years ago

Inside the component


const Users = () => {
  const [err, users, loading, refetch] = useFetch(`/api/users`, {auto:true});

  const onClick = () => refetch(...);

  return (
    <div>
      {users.map((user) => <User key={user.id} user={user} />)}
    </div>
  );
}
Quaquaro commented 2 years ago
const Home = React.lazy(() => import('./Home'));
const About = React.lazy(() => import('./About'));
function MyComponent() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Route path="/home" component={Home} />
      <Route path="/about" component={About} />
    </Suspense>
  );
}
Quaquaro commented 2 years ago
const [state, setState] = useState({
  count: 0,
  name: "",
});

const onClick = () => {
  setTimeout(() => {
    setState((prevState) => ({
      ...prevState,
      name: "John",
      count: prevState.count + 1,
    }));
  }, 1000);
};
Quaquaro commented 2 years ago
const getPrimaryColor = ({ theme }) => theme.colors.primary;
const getDefaultColor = ({ theme }) => theme.colors.secondary;

const Button = styled.button`
  background-color: ${getPrimaryColor};
  color: ${getDefaultColor};
`;
Quaquaro commented 2 years ago
return (
  <>
    {list.map((item) => (
      <React.Fragment key={item.id}>
        <SomeComponent />
        <SomeAnotherComponent />
      </React.Fragment>
    ))}
  </>
);