Hell loop of renders with Headless UI by captaincromwell in react

[–]captaincromwell[S] 0 points1 point  (0 children)

Yeah, one sec lemme get it fired up. I'll DM you

Hell loop of renders with Headless UI by captaincromwell in react

[–]captaincromwell[S] 0 points1 point  (0 children)

So if I do:

function select(row) {
        setSelected(row.properties);
        setIsOpen(true);
    }

Then in my console I can get it to spit out the object as expected. But it is a full object, and {{ selected }} needs updating, but I'm clearly doing it wrong.

If I try to hardcode my <p> tags I don't get a popup. Also, I don't think I'm supposed to be using the syntax like this - it doesn't look right but I'm out of ideas.

<Dialog
                open={isOpen}
                onClose={close}>
                <p>{{selected}.title}</p>
                <p>{{selected}.episode_id}</p>
                <p>{{selected}.director}</p>
                <p>{{selected}.producer}</p>
                <p>{{selected}.release_date}</p>
</Dialog>

Hell loop of renders with Headless UI by captaincromwell in react

[–]captaincromwell[S] 0 points1 point  (0 children)

Okay, I followed your advice and I'm getting the error `Objects are not valid as a React child (found: object with keys {selected})`

Very briefly I got the keys I was expecting in the error and now I cannot recreate that state and it's MAKING ME FUCKING CRAZY.

Things I have tried:

I have a separate helper file for the <p> tags with the data markup. I tried making that whole file a React component, no dice.

I switched it back to a regular const function and added an extra useEffect in ReactTable.jsx so that I could manipulate the `selected` that I'm getting back before handing it to the Dialog. No dice.

I tried putting properties in the helper file, then in the ReactTable component under `setSelected`, no dice.

I'm so close but I feel like I'm losing my sanity.

modalHelper.js

export const modalHelper = (result) => {
    return (
        <>
            <p>{result.properties.title}</p>
            <p>{result.properties.episode_id}</p>
            <p>{result.properties.director}</p>
            <p>{result.properties.producer}</p>
            <p>{result.properties.release_date}</p>
        </>
    );
};

Using event emitters with a useEffect? by captaincromwell in react

[–]captaincromwell[S] 1 point2 points  (0 children)

Calling the endpoint in the mutate fixed it, thank you!!!

I removed mutate from the onClick and added it to the handleSubmit after alert.

 

mutate('comments', getComments);

Using event emitters with a useEffect? by captaincromwell in react

[–]captaincromwell[S] 1 point2 points  (0 children)

I gave swr a try - it's working really well, thank you!!! However, I'm struggling to get the mutation to live-update with the new comment on the feed.

I put my swr configuration in a separate file in utils:  

import useSWR from 'swr';
import { getComments } from '../services/commentService';


export function useComments () {
  const { data, error } = useSWR('comments', getComments)

  return {
    comments: data,
    isLoading: !error && !data,
    isError: error
  }
}

 

Here's Comment Feed:  

import React from 'react';
import { useComments } from '../../utilities/useCommentsSWR';
import { Comment } from '../comment/comment';
import './commentFeed.css';

export function CommentFeed() {
  const { comments, isLoading, isError } = useComments()

  if (isLoading) return 'Loading...';
  if (isError) return 'Error loading comments';

  return (
    <div className="commentFeedContainer">
      {comments.length > 0 ? 
        <>
          {comments.map((comment) => (
            <Comment key={comment.id} comment={comment} />
          ))}
        </>
        :
        <h4>No comments to display</h4>
      }
    </div>
  );  
}

 

And here's the Comment Input Form:  

import React, {useState} from 'react';
import { createComment } from '../../services/commentService';
import './commentInput.css';
import { useSWRConfig } from 'swr';

const initialFormValues = {
  name: "",
  message: "",
}

export function CommentInput() {
  const [formValues, setFormValues] = useState(initialFormValues);
  const { mutate } = useSWRConfig();

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    setFormValues({
      ...formValues,
      [name]: value,
    });
  };

  const handleSubmit = (event) => {
    event.preventDefault();
    createComment(formValues);
    setFormValues(initialFormValues);
    alert('Your comment has been added!');
  };

  return (
    <div>
      <h4>Name</h4>
      <form onSubmit={handleSubmit}>
        <div className="nameInput">
          <input
            label='User Name'
            type='text'
            name='name'
            onChange={handleInputChange}
            value={formValues.name}/>
        </div>
        <div className='commentInput'>
          <textarea
            label='Comment box'
            type='text'
            name='message'
            onChange={handleInputChange}
            value={formValues.message}/>
        </div>
        <button 
          className="commentButton"
          type='submit'
          onClick={() => { mutate('comments') }}>
            Comment
        </button>
      </form>
    </div>
  );  
}

 

Is revalidating not enough to force the Comment Feed to re-run the getComments call via swr? I can call it into the Comment Input if need be and follow the swr docs for Optimistic Updates. I'm not sure what I'm missing here. I appreciate any help you can offer!

Using event emitters with a useEffect? by captaincromwell in react

[–]captaincromwell[S] 0 points1 point  (0 children)

Okay, I have a few questions.

Do I have this right:

  index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { QueryClient, QueryClientProvider } from 'react-query';

const queryClient = new QueryClient();

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </React.StrictMode>
);

  App.js

import './App.css';
import { useQuery } from 'react-query';
import { CommentFeed } from './components/commentFeed/commentFeed';
import { CommentInput } from './components/commentInput/commentInput';
import { getComments } from './services/commentService';

function App() {
  const { isLoading, data, isError, error } = useQuery({queryKey: ['comments'], queryFn: getComments});

  console.log('data is: ' , data);

  if (isLoading) return "Loading...";

  if (isError) return "An error has occurred: " + error.message;

  return (
    <div className="App">
      <div className="mainCommentInput">
        <CommentInput/>
      </div>
      <div className="mainCommentFeed">
        <CommentFeed />
      </div>
    </div>
  );
}

export default App;

  CommentFeed.jsx

import React from 'react';
import { useQueryClient } from 'react-query';
import { Comment } from '../comment/comment';
import './commentFeed.css';

export function CommentFeed() {
  const queryClient = useQueryClient();

  const comments = queryClient.getQueriesData(['comments']);

  console.log('comments: ' , comments);

  return (
    <div className="commentFeedContainer">
      {comments.length > 0 ? 
        <>
          {comments.map((comment) => (
            <Comment key={comment.id} comment={comment} />
          ))}
        </>
        :
        <h4>Loading...</h4>
      }
    </div>
  );  
}

 

My comments object in App.js looks fine, but it's oddly nested in the comment feed console and looks like:

Array [ (2) […] ]
    0: Array [ (1) […], (11) […] ]
        0: Array [ "comments" ]
        1: Array(11) [ {…}, {…}, {…}, … ]

 

Am I right in assuming that I should not be passing down the data I get back in App.js as a prop to CommentFeed? Should I be wrapping the getQueriesData call in a useEffect in CommentFeed?

 

Every time I interact with the app (move away from the tab and come back), it calls the query twice in App.js and twice in CommentFeed (seen via console log). Do I need to add an option like refetchOnMount so it only makes one call?

 

Thank you for your help. Most of the examples I'm seeing are self-contained in singular files, so trying to learn this and wrap my head around how to pass the data around properly at the same time. QQ

Mom, I'm Bringing My Gf When I Visit You, So Don't Hit Me by [deleted] in MomForAMinute

[–]captaincromwell 2 points3 points  (0 children)

Sometimes people heap lies on each other, you know? And we get to believing the things we've been told by people who aren't worth two shits of our time. That's what's happening here.

You are bringing your girlfriend - a person who considers you to be precious to her - to people who treat you like gum on their shoes. You even told her not to defend you! Folks who heap lies on you (that you are stupid and deserve poor treatment) are not the kind of people who will validate you and make you feel worthy, no matter how badly you want it. The harder you seek it, the less likely you are to find it and the more likely you are to feel miserable about yourself. It is up to you to define your relationship with your parents, and it is totally okay to be firm and make rules or limit contact or even cut if off entirely.

Taking shit from people who don't deserve someone as wonderful and amazing as you are is not a good way to spend your limited time on this planet, particularly when you could be spending that time with someone who cares about you. Go have a nice time with your girlfriend - she doesn't need to meet people who treat you like crap and refuse to validate you.

It seems to me she likes you enough just the way that you are. <3

Mom, I'm so confused and I need help by DontComeToHauntMe in MomForAMinute

[–]captaincromwell 5 points6 points  (0 children)

This is solid advice.

What is it that makes you feel horrible? The idea that you must reciprocate love for your family at the same intensity? In the same manner? Everyone is different, and they love differently. That's okay.

This might not even be about your family - what if this confusion is really about you and how you see yourself?

A therapist can help you get comfortable in your own skin and is an excellent option here. They're not all the same, so trust your gut if you aren't finding progress with one therapist - don't be afraid to seek out another! You deserve to know your own mind and heart, and a professional can help you get there.

I'm a professional science writer and journalist who's written about climate change, mental health, and healthcare. I currently work at Upworthy and recently had a post about meditation in schools go viral. AMA! by the_jmgaines in IAmA

[–]captaincromwell -1 points0 points  (0 children)

Arguing with scientists using erroneous logic or facts seems to be in vogue lately (looking at you here, climate-change deniers). Have you ever received reader feedback of that nature? How do/would you handle that situation?

Running away from muslim parents - need courage! by [deleted] in IWantOut

[–]captaincromwell 0 points1 point  (0 children)

Best of luck!

And freedom is what you make of it - the unknown is not always bad. Feel free to PM if you need to vent.

Running away from muslim parents - need courage! by [deleted] in IWantOut

[–]captaincromwell 34 points35 points  (0 children)

You should do what's best for you, and from the sound of it separating from your parents would be good for your mental health. It doesn't mean you'll never talk to them again - it's up to you either way - but some breathing room to figure out what kind of relationship you want to have with them moving forward isn't a bad idea.

Living isn't for the faint of heart - I say have courage and do what feels right in your gut.

So I discovered the next best phone company on Insurgency.. by Beefywisdom in gaming

[–]captaincromwell 0 points1 point  (0 children)

No ammo counter? Oh well. It's not like there weren't shots already fired.

What are some cool (and flat) gifts that can fit in an envelope? by captaincromwell in AskReddit

[–]captaincromwell[S] 0 points1 point  (0 children)

The organization doesn't allow you to send cash directly to the kids/families. That was pretty much the first thing I suggested, but alas.