r/learnreactjs • • Mar 16 '22

Question Child component not re-rendering based on state change?

6 Upvotes

The problem:

I have a child component called <UpdatesSection> which displays some content from my CMS (Strapi). The functionality is there, but not on the initial page render after I set my user's token to null using a logout function.

What I think is going wrong:

Strapi requires a token to be sent with -every- request and is user/role specific. To help with public content that doesn't require any specific role, I made a dummy "test/public" user that authenticates and gives the user a token if none exists. This functionality also works just fine. I think what's happening is that the child component useEffect fires off first, then dummy login function runs in my App.js useEffect, meaning that the token wasn't set before the child component made its request for data from the CMS.

What I've tried:

  • Passing my userToken state down into the dependency array so that when the token changes, the component should render again.
  • Setting a default public token at logout.

Here's my code - https://gist.github.com/mlghr/1486841e73a566a1876bd3b6903eb939

Am I missing something about how components conditionally re-render?


r/learnreactjs • • Mar 16 '22

Question How do you remove git tracking from react app on windows? rm -rf .git is not recongized

2 Upvotes

recognized* I am in the root directory and tried replacing the first command with del and -rf with -f

Any help is appreciated


r/learnreactjs • • Mar 16 '22

Learn React.js with Full Project (React Hooks & React Router) | React Crash Course

Thumbnail
youtu.be
5 Upvotes

r/learnreactjs • • Mar 16 '22

Question Node Backend with React

7 Upvotes

I am a mostly self-taught front-end developer that has somehow managed to get a code challenge for a potential job. Unfortunately the challenge is filled with things that I have little-to-no experience with. Most of my (very little) experience is purely front end, with React and a few other technologies, but I'm being asked to set up a Node backend with the React front end, in Typescript (zero experience with).

The challenge seems mostly straight forward. I have to create an application that "allows a user to manage a collection of "items"; "items" must have at least 6 properties/fields"

I mean, this is basically a todo list, with some extra features they're asking for. I managed to get it running with MongoDB.

One of the things that's tripping me up is one of the item properties they want is "A property picked from a list of options provided by the backend"

I'm thinking this means like a dropdown menu, where to the options are provided by the backend? How would you approach this and is there some documentation that would help with this?

Sorry for the rambling, my mind is kind of everywhere right now.

Also, apologize of this should be posted somewhere else.


r/learnreactjs • • Mar 15 '22

Question How to build a map-builder?

6 Upvotes

Hey gang!

I want to make a map-maker where:

  • A user can drag and drop tiles to create a map.
  • When they drop the tile they should be able to rotate it They should be able to add pinpoints on top of the map that will be unique.
  • They should be able to save the map and edit later.

So for example I can make a river and some road, and then add a marker to the road. Save, And later add a mountain and another marker.

I've seen some interesting tools like leaflet but do you have any ideas where I can get started with a tile-based drag n drop build that can use my own PNG or SVC images?

Thanks so much!


r/learnreactjs • • Mar 09 '22

Question about React Router v.6

4 Upvotes

Hey folks!So I am using React Router v.6 and I am building a shop app which has to display a store page and a product page.The StorePage component holds a list of ProductCard components which serve as a preview component for the product, when the user clicks on the ProductCard, they are being navigated to the ProductPage. Here comes the confusing (for me) part. When I use navigate() I pass in 'product/:productName', {state : {productId}}, let me explain - the first parameter is the location I want to go to, yes, I want to display the name of the product in the query instead of the id of the product, I don't want to display the product id there and it is not an option at the moment. What I do now is I take the useLocation hook inside of the ProductPage component and I access the second parameter that I passed - state. From the state I take the product id and call my API with it so I can get the product info and display it on the page.Problem is - when I copy and paste the link in the browser for example "mysite.com/product/iphone-13" I get an error in the console stating "unable to read productId of null" because my state does not exist! So I figured I have to either pass the productId as a prop to ProductPage, which is not possible with my current architecture or I need to pass the productId to the query.Do you have any idea how I can avoid displaying the product id in the query without breaking my page when the link is directly pasted in the browser? Any help will be appreciated!

Here's an example component structure (not actual code):

<StorePage>

{products.map(p) => <ProductCard key={p.id} onClick={navigate('product/${p.name}, {state: {productId: p.id}) />

<StorePage/>

<ProductPage> const productId = location.state.productId; //this gives the error when pasted into the browser <ProductPage/>


r/learnreactjs • • Mar 07 '22

Question Creating reusable text field component using Material UI and react-hook-form

4 Upvotes

I was trying to develop reusable text field component using Material UI and reach-hook-form. I was referring following examples:

  • Example 1 ref :
    type FormProps<TFormValues> = {
      onSubmit: SubmitHandler<TFormValues>;
      children: (methods: UseFormReturn<TFormValues>) => React.ReactNode;
    };

    const Form = <TFormValues extends Record<string, any> = Record<string, any>>({
      onSubmit,
      children
    }: FormProps<TFormValues>) => {
      const methods = useForm<TFormValues>();
      return (
        <form onSubmit={methods.handleSubmit(onSubmit)}>{children(methods)}</form>
      );
    };
  • Example 2 ref :

After analyzing both, I came up with following:

 import { TextField } from "@mui/material";
 import { Controller, UseFormReturn } from "react-hook-form";

 interface IRhfTextBoxProps<TFormValues> {
   name: string;
   methods: UseFormReturn<TFormValues>;
 }

 // export const RhfTextBox = <TFormValues extends unknown>(props : IRhfTextBoxProps<TFormValues>) => {  //##1
 export const RhfTextBox = <TFormValues extends Record<string, any> = Record<string, any>>(  // ##2 similar to example 1
   props: IRhfTextBoxProps<TFormValues>
 ) => {
   return (
     <Controller
       control={props.methods.control}
       name={props.name}  // ##3
       render={({ field, fieldState, formState }) => (
         <TextField
           error={!!fieldState.error}
           helperText={fieldState.error?.message ?? ""}
           key={props.name}
         />
       )}
     />
   );
 };

Both lines ##1 and ##2 in above code gives following error at line ##3:

Type 'string' is not assignable to type 'Path<TFormValues>'.

The detailed error message is as follows:

The expected type comes from property 'name' which is declared here on type 'IntrinsicAttributes & { render: ({ field, fieldState, formState, }: { field: ControllerRenderProps<TFormValues, Path<TFormValues>>; fieldState: ControllerFieldState; formState: UseFormStateReturn<...>; }) => ReactElement<...>; } & UseControllerProps<...>'

Q1. Why am I getting this error?

Q2. Should I just use non generics FormInputProps as in example 2, instead of generics based FormProps in example 1.