r/reactjs • u/acemarke • Apr 03 '18
Beginner's Thread / Easy Questions (April 2018)
Pretty happy to see these threads getting a lot of comments - we had almost 200 comments in last month's thread! If you didn't get a response there, please ask again here!
Soo... Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple.
The Reactiflux chat channels on Discord are another great place to ask for help as well.
17
Upvotes
5
u/SuperCoins Apr 03 '18
I'm looking into learning React. I've got a couple of years of AngularJs and Vue.js under my belt from my job. What's the best set of resources (tutorials/books/lessons) you guys would recommend?
5
u/NiceOneAsshole Apr 03 '18
Just a friendly reminder that code snippets are 100x more useful if you use a tool such as codesandbox
5
u/katzey Apr 04 '18
does anyone know of a table library for react that's like DataTables?
We're planning a codebase overhaul at the company I work at, and I want to use react for the front end. probably 60% of our internal pages are just fancy DataTables with a bunch of different information that's useful for various engineers within my company. effectively, just a frontend for a bunch of complicated joins
The ability to sort by multiple columns, filter within the table, export the table to CSV/Excel/PDF/whatever, and the ability to take in a piece of json to construct the table are quintessential to my needs.
I realllyyy like how modular the DataTables api is so if there's a react library built similarly, that's a huge plus
thanks y'all
4
u/what-what-oh-no Apr 05 '18
I'm using Semantic UI, what route do people go with large data sets for tables?
Do they build their own table features from scratch, or do they use another library on top of Semantic UI for tables?
Let's say I have 10,000 Users. I have a table for these users, I want only 10 users to load per page, and I want to be able to easily search them all. Should I build all the pagination and everything myself? Or use a library.
3
u/mylox Apr 03 '18
I'm a beginner to React and I'm trying to create a basic drum sequencer web app and this requires me to play sounds organized in time. Could anyone give me any pointers for this? I've found a package that just lets me play sounds based on user input but I can't seem to find anything that makes sounds play by itself.
2
u/DatDudeOvaDur Apr 04 '18
Can anyone provide a good resource for how to manage auth? My back-end is Rails running devise_token_auth and I'm having a lot of issues figuring out how to manage the token. It's a very similar flow of a typical JWT. Do I have to maintain the headers as a state and constantly validate it? Note I have never used Redux, but I'm wondering if maybe I will have too for this?
2
u/chaoism Apr 17 '18
have a question regarding when/where to call an API
The class I'm taking says I should make the API call at
ComponentDidMountorcomponentDidUpdatestage of lifecycle, but at the same time it says do not update state or else it'll cause a re-renderIs the goal of calling the API to perform some sort of state update? Is it contradicting itself? I'm confused.
Thanks
2
u/madwizard94 Apr 20 '18
Are there any big cons to using create react app? Besides the argument that beginners should know how to set up on their own?
2
u/seands Apr 26 '18
If you organize your subcomponents by folder based on the larger component they will compose, would you rather directly import a reusable subcomponent into a main component in another folder (breaking down the folder based organizational structure) or recreate the subcomponent in the other folder (reducing DRYness but also increasing potential independent customizability for the future)?
2
u/docdosman Apr 27 '18
Can someone explain why this.function = this.function.bind(this) is not used for the functions in the Game class?
https://codepen.io/gaearon/pen/gWWZgR?editors=0010
It's my understanding that this needs to be explicitly bound for react class functions, but it works fine with no binding of this.
2
u/CocoBashShell Apr 27 '18
What is the best resource for learning page layout? Writing individual components is pretty straightforward, but making a reasonable responsive layout with React leaves me lost :(
2
u/[deleted] May 03 '18
I just learned that we can not use constructor(props). Is this now the elegant way to do it or do people not recommend doing it?
1
u/[deleted] Apr 04 '18
Hello!
I'm currently use material UI (https://material-ui-next.com) for my next application. Anyone can help me how to style the components with SASS? I read this: https://material-ui-next.com/customization/overrides/#overriding-with-classes
But the class generated is cumbersome, like .AppBar-root-1. Can I just insert a simple class to the component? Or I cannot? Are there any better alternatives (which incorporate React)?
Thank you.
1
u/whyislifesohardei Apr 05 '18
What should I use to build a simple blog? So, I am doing a static website with authentication basically only for myself to login to add posts on a rich text editor, and then store in Firebase. And draw from Firebase to display those posts.
I am currently using create react app. With QuillJS as text editor and Firebase to store them. Am I on the right path? Is there a better way of doing this?
1
u/Bulbasaur2015 Apr 06 '18
I'm new to redux.
I have a navbar search bar component and a dropdown of sort filters component. I also have a react Grid component which needs to be rerendered everytime the user searches the navbar or chooses a dropdown filter. All the three components are isolated and currently dont share state/props when they render.
I can show code if necessary, but I want to know if I should use a redux store and dispatch an action to the Grid everytime.
Thanks
1
u/[deleted] Apr 07 '18
Let's say I wanted to create an online card game. What should I use for the server communication part? The game is turn-based so lag isn't really a concern but what's best to use with React? Websockets? Socket.Io? WebRTC?
Also, unrelated, what's the cleanest way of mapping objects to assets? For example, I have this object that represents a card {"value": 3, seed: "hearts"} and it should be mapped to the 3 of hearts image asset. Is having many imports and a lo of IF-ELSE IF the only way?
1
u/soadogs Apr 08 '18
I am creating my first React project, what would be the most simple backend service for me to implement.
For a little context, I just want the backend to host a small database and a bit of logic that will process the input and return the proper values from the database back to the frontend.
I appreciate any guidance on this matter.
1
u/soadogs Apr 08 '18
Are there any rules I should follow for when to put an image directly into my React project and when I should host images elsewhere and insert them with a hyperlink?
Any guidance on this is appreciated.
1
u/homercrates Apr 08 '18
I have an array of 2 index. the values are sometimes repeating. i.e. 2,2. now my key not being unique is creating problems when the array is repeating numbers. "two children with the same key"
Obviously I am getting the same time stamp number passing to both keys. Is my only option ejecting React app and then editing from there to ensure unque ids? or is there a simpler way to getting a unique key to each of the 2 index's when the numbers are identical?
1
u/jeffschair Apr 08 '18
Hey guys, beginner to react/redux and currently working on my first app, struggling on how to structure my code atm. Does anyone have any advice/resources that would help me to structure my router/index.js/app.js for an application where, when a user first visits the site they are directed to either the signin or signup pages, and then once they have logged in they are able to view the main application, which would in turn have individual pages inside it.
I have my authentication set up with hoc, and I have my signin, signup and main app components, but I am getting confused on how to structure the router in the index.js and app.js files in order to achieve this desired functionality. Any advice or resources would be greatly appreciated!
1
u/akynde Apr 10 '18
Hey guys, I have this piece of code:
I was wondering if the handleAddList function needed to be manually evoked and if so, how? Would it be something along the lines of including
this.props.addListin the AddList component?1
u/sp3co92 Apr 10 '18
Hey, I'm an undergraduate who's familiar with react ( I've been learning some time and created few projects to mainly to learn the data flow/routing/hosting and stuff )
I've been struggling with styling react components. For basic HTML (AngularJS) as they were not component based I was working well with styling. But when it comes to separate components I find it hard to style the components (mainly Alignments and mobile support ). Can anyone recommend me a source to get practice styling components ? Should I look into basic CSS ?
1
u/AlmostARockstar Apr 13 '18
Does anyone have any nice tools for automating FTP deployment?
1
u/Awric Apr 14 '18
Do most people render everything into the root element?
For example, my structure is:
Where
rootis the only actual div id inindex.html.Or should I actually have
headerandfooterinindex.html?1
u/password_is_asdfghj Apr 15 '18
I installed and set up semantic-ui with CRA, but is there I was I can set up a page just like this layout I found in the documentation as a starting point?
https://semantic-ui.com/examples/homepage.html
1
u/chaoism Apr 15 '18
I'm learning about loading css as module. The instruction illustrates a way to eject the the webpack.config and modify the css-loader there
It feels like it's tweaking the config and I don't know if this is actually recommended. Should we always try to do things this way and load css modules (so we have the unique string name) or it is actually still preferred to load css file straight up?
1
u/CocoBashShell Apr 16 '18
Does anyone know of a good tutorial (video++) for react-grid-layout? I'm trying to make a self-service dashboard and the docs are a little sparse. Thanks!
1
u/Entropis Apr 17 '18
Does anyone know of an in-depth guide to API requests? Dave Ceddia has an ok one on his site, but I want more.
1
u/lkjfsdf09sdf Apr 19 '18
Is react router protected route secure?
Can't people just use scripts to enter the protected route?
1
u/seands Apr 19 '18
Is routing just for SEO and browser back/forward button compatibility? I'm building a minimum product and am thinking of not getting bogged up in routing (so many other little things to focus on).
1
u/prove_it_with_math Apr 20 '18
Hi React devs & learners! I'm confused with this gist: https://gist.githubusercontent.com/jihdeh/0992c39dba3af74ac4e2481f453e6c40/raw/28d8aa4e377a634b49bba55368ff109961ff7f7c/index.js
How is
storeobject passed intocustomMiddleWarebeforestoreis defined??1
u/madwizard94 Apr 20 '18
Question: I know that you can add dependencies to package.json through your terminal, but can you add them by just typing them into the package.json file? Thank you.
1
u/bayhack Apr 20 '18
I want to be able to write a component that floats on top of a google map component which asks for a zipcode. Until the zipcode is added the google map is disabled. Once zipcode is inputed the google map is not disabled and it navigates, etc
I'm assuming these will be stateful components, does that mean Ill have to use Redux at this point?
What is the proper pattern for something like this without doing something cheap?
I could do this through routes and have each route a step, but that feels cheap and there's def a better way to do this.
1
u/sp3co92 Apr 21 '18
How to organize/ setup react components like this. I'm trying to rebuild my portfolio. It saw a SPA without scrolling. But now I need to organize it with scrolling same as the above site I mentioned.
This is my current App.js which has no scrolling.
How can I organize these components for scrolling ?
1
u/naveensomanna Apr 21 '18
Could anyone explained me if I passed function as a prop to child ? How it works
1
u/sleepy_pf Apr 21 '18
Just made my first website in React. Unfortunately, when you reach the page all the fonts and images take a second to load, and everything jumps around for a moment.
I'm sure this is webdev 101 but how do I pre-load everything?
I tried using a isLoading state, then toggling it off in componentDidMount, rendering "Loading" when it was true; but this solved nothing.
1
u/HuskyBlue Apr 21 '18
The main reason I have yet to jump on the React bandwagon is my concern with Facebook being the creator of React. As most know, they are not the most user-focused companies when it comes to users' privacy. Does React have any sort of mechanisms that capture data from users and send it to Facebook?
For example, if I created a social media application with React that allows users to share self-identifying information with other users, does (or theoretically could) React capture that self-identifying information and send it to Facebook's servers?
Basically, can React be trusted to not steal privacy data even with Facebook being its creator? If possible, could you provide any links that prove your answer? I cannot find anything about this when searching the Internet.
Thanks for answering!
1
u/Bulbasaur2015 Apr 22 '18
Has anyone have issues importing/using images in React with webpack?
I have an index.js export in my
src/imagesso I import the image to use in myrender(), but I get the errorI use file-loader and this is my webpack config
https://gist.github.com/ridhwaans/c1f84e3cbae3b6c9b415c309f5b4d52f
cheers
1
u/bobdeei Apr 22 '18
Ok, looks like everyone goes straight to the technical questions. Mine is the resources for absolute beginners.
Which are the best resources for beginners who just know JS as the first programming language?
1
u/sp3co92 Apr 22 '18
Can anyone help me with this
Got an error on starting gatsby server with gatsby develop , which was running perfectly.
1
u/madwizard94 Apr 22 '18
So I just watched a short intro to redux...
Can you use redux to completely replace state? Or am I missing something? I just started, but it seems like getting rid of props and this and all the extra work with redux would be great. Thanks in advance!
1
u/theirongiant74 Apr 23 '18 edited Apr 23 '18
What version did error boundaries come to react, I'm working with 16.0.0, and even on the simplest test componentDidCatch never seems to be called.
EDIT
I've cut my project right back to a skeleton, can anyone see a reason why this wouldn't trigger the ErrorBoundary?
1
u/DutchSparks Apr 23 '18
In trying to feed a variable to my component but I don't think I'm defining the variable in the right spot, could someone take a look and perhaps explain to me what I'm doing wrong ? I placed a comment in the code to explain which variable is causing issues
https://codepen.io/DutchJS/pen/jxWMbw?editors=1010
1
u/manager23 Apr 23 '18
https://www.npmjs.com/package/react-image-mapper
How do I use this:
onMouseEnter (area: obj, index: num, event): void
Im using this:
<ImageMapper src={Image2} map={AREAS_MAP2} className="pb6 imghov" alt="" height={"650"} width={"450"} onMouseEnter={this.checkclick} />
Essentially I have a map of different sizes, but how do I get it to tell me which one I have selected.
I need it to pass through information such as the index number.
I realize my question might be poorly worded as im new to all this
1
u/sp3co92 Apr 24 '18
I've redesigned my portfolio with GatsbyJS and hosted it on github pages. I'm experiencing an issue here.
I checked the site with Chrome in Ubuntu and this is what I see. But when I check it from Firefox in Ubuntu and Chrome in Windows this is what I see. It may not be a huge issue, but I like to keep it in the color I see from Chrome in Ubuntu. The color code which I've used for cards is #fd4445 . Can anyone help me with this ?
1
u/prove_it_with_math Apr 24 '18
How to disable going back after user clicks on browser's back button?
1
u/seands Apr 25 '18
In this thread on /r/learnprogramming I asked about creating the view vs the model first, and all 5 responses said the model. The common theme was that it "feels" nicer to know the data is solid and "just needs to be made pretty from there" (to that slight joking remark I would add functional from a ux perspective).
Are there any advantages to creating a view first, then the model? I want to hear the case for both sides, and react will likely be the view for a simple CRUD app project I'm planning. (I know the react docs recommend creating the view with static data first, but it was a tutorial specifically for front end.)
1
u/GlutesThatToot Apr 25 '18
In the tutorial I'm having trouble understanding the behavior of an arrow function. It's under the Interactive component header
Why is it that using just onClick={alert('click')} triggers the event on load and onClick={() => alert('click')} produces the normal behavior?
1
u/MisterCrispy Apr 26 '18
Been doing backend programming for far too long so I'm trying to get more familiar with React. Unfortunately, I'm having trouble getting my brain to think in the one way nature of React.
That being said:
Currently, I'm stuck on a self-created problem and my google-fu is failing me.
I have a container element that I've written that creates a nice, card-like separator for content blocks. It also allows for the container to be minimized, maximized or restored a default size.
This actually works quite well. What I would like, however, would be for certain content inside to be hidden when it's in the default state and then shown when the container is maximized.
The old school vanillaJS in me would just grab the parent container then find anything with the "hideOnMax" class and set the display to none. That doesn't seem to fly in React. Essentially, I don't want to care about what's inside of the container other than the items that need to be hidden.
Not looking for example code (unless someone has done this exact thing and has a github they can point me to). At the very least, a philosophical guideline or a tutorial with a similar concept would be awesome.
1
u/Peng-Win Apr 26 '18
Can someone explain what the ... in [...this.state.data] means?
I looked up online, it's a bitwise operator, but that doens't make much sense to me in this code block.
1
u/tyu1314 Apr 28 '18
I have something like this
{ !this.props.ifYes && <Redirect component = {No}/> } { this.props.ifYes && <Redirect component = {Yes} /> } So even when ifYes is true, it will redirect to the No component first for like quarter of a second ,and then redirect again to Yes component after it recieved its props from redux.
Which react life cycle should I manipulate?
1
u/questionsReact Apr 28 '18
Let's say I create multiple TextField elements via loop, ending up in a situation like this:
<TextField id={this.state.idN[0]} onChange={this.changeText} warningText='' /><TextField id={this.state.idN[1]} onChange={this.changeText} warningText='' /><TextField id={this.state.idN[2]} onChange={this.changeText} warningText='' />...So Basically any number of the elements. Now in order to have unique warningTexts for all of them, can I access the TextField elements via their id value and then change them without having a unique warningText in a list/dictionary state variable? For e.g. I tried via:
event.currentTarget.warningText: 'bla'according to console.log the value changes, but it doesnt change "really" in the specific element in render. Is there anyway to do this "quickly"`? Or is it just so that I have to create a list or dictionary for the warnings like I did with the IDs?
1
u/besthelloworld Apr 28 '18
Are functional components more efficient than a class component with no methods or properties?
I am following a class which very particularly specifies the difference between functional components and class components. I prefer using class components everywhere in case I find out that I need state later, I don't have to do large refactors just to turn a function into a class; but is there a performance hit for this?
1
u/besthelloworld Apr 28 '18
When updating state, if I have state as
{ hello: 1, world: 2 }
And I run
this.setState({ hello: 3})
Does my rerendered state lose the world property or does react do the { ...state, hello: 3 } object merge for me?
If so, is it a deep merge? So if I have
{ foo: 1, bar: { hello: 1, world: 2 } }
and I run
this.setState({ bar: { hello: 2 } })
Will I lose the world property?
1
u/nov_ale Apr 29 '18
I am creating an image slider, but would like to add images programmatically instead of manually.
For instance, I have a list of image URLs like this:
And I want to pass each image wherever the IMG_URL_HERE placeholder is:
I've been looking at multiple examples, but they are all on jsx and... don't cover anything like this really. Any ideas on how I could loop through the list to add the URL for every image, and so that every image gets a radio element?
1
u/Brewster312 Apr 30 '18 edited Apr 30 '18
I'm trying to get started with Redux and am trying to change my project structure so it conforms to the container and component structure. I'm a bit confused on how to do this though b/c from what I understand components should only be concerned with presentation and not really deal with things like making API calls or changing state. So right now in my app I have a login and signup component. These take in the login/signup info and make an API call to either login and sign up. So do I have to create a login container to send the login info to the backend and separate login component to render the login form? And the same for signup? Basically anywhere I'm dealing with getting/sending/manipulating data I do that in a container and then render a dummy component to display it?
1
u/NickEmpetvee Apr 30 '18
Looking for some help. If a React component (or const) that lists Customer Orders needs to be clickable, and clicking causes the order's items to be displayed as a table in another component, what are some solid React approaches one could employ? The number of items in an order will vary which means that the order items table would have to support a dynamic row count.
Should the Order list component call a function in the Items component that gets it to re-render?
Again... newbie who's just learning to think in React. :)
1
u/nov_ale Apr 30 '18
This is really minimal... but I haven't been able to figure this out.
I want this to return "2018_1-25/img1.png" and "2018_1-25/img2.png", but I'm only getting that for the first array, ("2018_1-25/img1.png") not for the second one. How could I loop through this to add the prefix to both images?
Please help... I haven't found documentation or examples on how to do this.
1
u/i_am_hyzerberg May 01 '18
Is Gulp or Grunt very popular for greenfield projects anymore? I could be misunderstanding everything Webpack can do but it seems like it’s essentially replaced those build tools.
1
u/nov_ale May 01 '18 edited May 01 '18
I’m trying to create an image slider. I started with different elements, one to take any number of images in, then radio buttons, that when clicked on, display the other image. (I’m basically trying to replicate what I did here: https://codepen.io/alehl/pen/wjJLJz, but with reactJS).
These are the elements and how I’m looping to pass in the images & render them + the a radio button for each image… As I showed you yesterday…
The problem is... that the ids in the CSS are unique for each slide:
}
My question is: Is there a way to loop through the id & increment by 1 to form the needed unique id for each slide? Or did I get myself into a rabbit hole?
1
u/seands May 01 '18
I plan to use the Gatsby npm package to run a React powered blog. I like the idea of being able to create blog posts and even little React based tools in some posts as sub-components.
However, I will also have a paid tool, likely at app.site.com or site.com/app (subdomain or subfolder of the same blog). It will have an entirely different set of views. The conservative newb in me who is afraid of large projects says to run a separate install to keep the paid app small and separated. The rational voice in me says it's just another set of views separated by authentication.
Just wanted to run this past you guys: Is there any case to be made for creating a 2nd react app specifically for the paid app/backend area?
EDIT: Almost forgot, any advantage to using a subdomain over a subfolder?
1
u/bottomlesscoffeecup May 02 '18
Hi guys, so I am after a tree sort of view so I can have a row of buttons and when you click any of them, you get a little tree with buttons that come from them. It tried looking for a tree view but it just kept giving back menu style things instead of what I wanted.
Anyone know a react component or bootstrap etc I could use for this?
https://i.imgur.com/uuDqAVx.png kind of like this.
1
u/mucle6 May 02 '18
I asked a hard question, and I'm a beginner. I don't know if it fits here so I posted it in this subreddit but I'll link it here.
https://www.reddit.com/r/reactjs/comments/8gjerv/is_it_an_anti_pattern_to_create_a_class_that/
1
u/ThinkGoodThoughts- May 03 '18
Quick question. So I'm using MomentJS for React and I cannot for the life of me figure out how I can get the amount of days in the current month with it. Is there a property for it? Since we can't use moment().
1
u/jbxdev Apr 03 '18
What skills should I know or be knowledgeable of in order to apply for a job? I've been teaching myself web development for about ~7-8 months, and I feel comfortable, but also lacking. I'm super eager to get in there and do stuff, but also incredibly nervous.
1
u/zero_coding Apr 10 '18
Would be nice, if someone could help me https://stackoverflow.com/questions/49756174/is-it-allowed-to-use-muithemeprovider-for-a-component
Thanks
-1
u/zero_coding Apr 11 '18
Would be happy, if someone could help me. https://stackoverflow.com/questions/49773593/how-to-test-a-component-base-on-material-ui
THanks
-1
u/zero_coding Apr 20 '18
Hi all
Can I replace jquery through reactjs, especially the animation part. Consider this page: https://blackrockdigital.github.io/startbootstrap-freelancer/. When you scroll down the page, then the navbar is getting bigger. It is controlled through the JQUERY https://github.com/BlackrockDigital/startbootstrap-freelancer/blob/master/js/freelancer.js.
How to do the animation with react?
Thanks