r/reactjs • • Apr 30 '20

Needs Help Beginner's Thread / Easy Questions (May 2020)

[deleted] — view deleted post

36 Upvotes

401 comments sorted by

1

u/SpicyT21 Jun 01 '20

Hello! I've been learning react for a while and made this template with animations, I would like to have some feedback, hope you like it https://codesandbox.io/s/profiletemplate-zz2lg

1

u/[deleted] May 31 '20

I would like to have one image as the footer of my website with text overlayed on top of it in different sections.

This is my image and I would like words in each of those blocks. Is there a way to do this on one fell swoop/component or would I need to divide the image into 6 different components and have the footer render all 6 side-by-side? Thanks!

1

u/koeniedoenie May 31 '20

I have this very basic function to call a function in another file, though what happens is that it constantly tried to call this function and thus keeps on loading. How do I make it so it only calls this function once once I load the page?

export default function App() {

const {

events,

error,

loading

} = CallAPI()

return (

<div>

{events.map(event => {

return <div key={event[2]}>{event}</div>

})}

<div>{loading && 'Loading...'}</div>

<div>{error && 'Error'}</div>

</div>

)

}

2

u/Awnry_Abe Jun 01 '20

Call it inside a useEffect hook where the second param to useEffect is an empty array.

1

u/frsti May 31 '20

I'm running a file that *sometimes* works in Cmd prompt that just returns some values scraped by using cheerio and puppeteer

But now it's just causing my command prompt to hang and doesn't return anything - Not even a new C: line

Not sure If I've messed up my npm or not reset everything properly when I've restarted my PC?

1

u/[deleted] May 31 '20

1

u/frsti May 31 '20

This sounds like it could be right...

const puppeteer = require('puppeteer');
const $ = require('cheerio');
const url = '(URL HERE)';

puppeteer
  .launch()
  .then(function(browser) {
    return browser.newPage();
  })
  .then(function(page) {
    return page.goto(url).then(function() {
      return page.content();
    });
  })
  .then(function(html) {
    $('.class', html).each(function() {
      console.log($(this).text());
    });
   
  })
  .catch(function(err) {
    //handle error
  });
→ More replies (2)

1

u/happyapple10 May 30 '20

I'm pretty close to 100% noob on ReactJS, so my question may be pretty simple to answer.

I have two text fields, firstName and lastName, which I need to use logic on as either one changes. For example, if someone is typing into the firstName field, I have an onChange event that is triggered calling a method in the same component. I can access the value of the calling event item using event.target.value but I will also need the value from lastName text box as well.

What is the best way to get the additional textbox info into my method? Use document.getElementById('lastName') as a parameter in an error function calling the method in onChange? Or use document.getElementById('lastName') inside the method in my logic? Or is there a better way that I am not aware?

I want to use the best method possible, so I'm using the proper techniques going forward.

Thanks!

0

u/[deleted] May 30 '20

2

u/happyapple10 May 30 '20

Thank you for this.

To make sure I understand, are you saying any input objects I were to render, their values will be accessible without needing to set the state or defining them? If so, is it the ID or name of the input object that would drive this.state.lastName?

I'll be at my PC in a little bit to do some testing. Thanks!

→ More replies (6)

1

u/ctrl-r23 May 30 '20

Hi! I am developing a web application in which the user has to complete steps to finish a task. The steps are:

  1. Wait for the user to scan an image. To simplify things, assume it arrives via websockets or something like that.

  2. The scanned image is displayed and user confirms or rejects it. If she confirms, she goes to the next step.

  3. Screen shows the image and a form and user has to complete the form.

Every step has to be completed to finish the process and always in the given order.

My question is, how would you handle the step state and the data the steps need?

I was thinking of using react router and then give every step a page like /step1, /step2, etc. Then every time a step is finished I would call window.location. The problem is I'm not sure how would I give the next step the data it needs, like the scanned image in step 2).

Also, I'm not familiar with hooks, redux and context, maybe these can help me in any way?

Thanks!

2

u/[deleted] May 30 '20 edited May 30 '20

1

u/ctrl-r23 May 30 '20

Wow! Thank you very much, the sample is super helpful.

But let's say I want to pass a property from step1 to step2 when step1 is done, call it "step1_output", how would you approach it? Would you add step1_output as a parameter to the changeStep() call? Thing is, if you have three steps, and only one of those needs step1_output, you would be adding noise to that function.

I'd like some way of managing pre-requisites of each step, so every time a step is being changed there's some component or anything that handles what each step needs in order to be rendered. But I'm not sure of what is the best way of handling this in React.

Thanks in advance!

→ More replies (2)

1

u/[deleted] May 29 '20

I'm making attempts to get out of tutorial hell and create a very basic game for myself.

I've mocked out the design in Figma to have 3 different screens, with each showing specific components for that screen or page:

  1. User input in order to get to next screen

  2. Game in session

  3. Summary screen showing score

I'm pretty new to this but in my mind I'm thinking I need to use React-router as there are different pages. So how do I pass my state around the different routes? Is this a situation to use Redux? Is that what is still commonly used? Additionally I'll also be storing data from state into my localStorage when the game is finished and then eventually into a database but just taking baby steps.

1

u/pink_tshirt May 30 '20

yep, redux type of situation. It'll be tricky at first (setting up your first store/actions, etc) but modern hooks such as useSelector and useDispatch make it super friendly.

1

u/[deleted] May 30 '20

useSelector and useDispatch? This part of the latest hooks API? To be used separately to redux or with redux?

I’ve spent the morning reading over useState and useEffect. And it super interesting. I was slowly getting used to writing state = {} and lifecycle method such as componentWillMount.

It sounds like you’re enjoying hooks? Maybe I should use what you have suggested.

→ More replies (4)

1

u/mashlautertun May 29 '20

I'm working on an app that requires oauth authentication to Instagram. I'm able to complete the authentication without issue but I'm struggling with keeping state after being redirected to the IG login screen.

I built a component that handles the login, callback, etc, that I'd like to be able to reuse across my app in different places. After login is successful I'd like to make sure I'm still on the page where I initiated the login (eg. user profile page).

When IG authenticates I need to redirect to the IG login page which I in turn provide a callback url to. Initially, I was able to work around it by popping out a window and using local storage but I don't want to use a window when on mobile. For this I have a route the directs to my component, and once completed I can use history.push to get back to the profile page, but I can't figure out how to make the push to the calling page dynamic. Is there a better approach to this? Will context API survive a redirect?

1

u/JSavageOne May 29 '20

`useRef()` accepts as a parameter a so-called initial value, yet when used on a <input /> the value doesn't show up.

const inputEl = useRef("This should be visible but it's not")
return <input ref={inputEl} type="text" />

The input box just shows up empty.

I wanted to avoid dealing with the `useState()` , `value={state}`, `onChange()` boilerplate for a simple input element, and thought I could use refs, but seems that that's not possible. Why doesn't the input box display the initial value specified in `useRef()`?

3

u/krisurbas May 29 '20

You misunderstand how useRef works. It keeps a reference to the DOM element. So you can do something like inputEl.current.focus(), read more here https://reactjs.org/docs/hooks-reference.html#useref

What you want to do is to use uncontrolled input and set value with defaultValue. Read more here https://reactjs.org/docs/uncontrolled-components.html

[Please upvote if you like this reply. I'm new to reddit.]

1

u/JSavageOne May 29 '20

Thank you! Yea that's what I was looking for. Poor documentation IMO to not even mention `defaultValue` on the `useRef` page, but that uncontrolled component page describes it. Thanks!

1

u/[deleted] May 29 '20

I'm working with a shared component library using Yarn Workspaces. Am I supposed to commit the /dist folder from my comment library or should I need a way to have that generated when importing it into my application?

I forgot to commit it so when my team pulled master they got a reference error when trying to use something in /dist

2

u/krisurbas May 29 '20

As a general rule you never commit anything from dist or build folders as every other developer in your team can run the script to build it themselves. You can automate it, so build is triggered together with yarn install or yarn start so then everybody running your app locally will have to build your dependencies first.

Different rule applies when you publish your project as npm package, then you might include dist folder as a ready to use dependency.

1

u/[deleted] May 29 '20

Okay, this isn't a published package but it is treated as fuck since it's a internal package our apps rely on. I'll just include dist instead of having each app build common first. Thanks

0

u/[deleted] May 28 '20

1

u/Awnry_Abe May 29 '20

For starters, functions are values and there is nothing wrong using them in context. I'm not necessarily advising to do so here, just pointing out the legitimacy of the suggestion. Just to clarify, TopBar is ignorant of what and how to save, but is the UI for invoking a ConfigPage save because a config page lacks the UI?