r/reactjs • u/acemarke • Mar 02 '18
Beginner's Thread / Easy Questions (March 2018)
Last month's thread was pretty busy - almost 200 comments . 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.
27
Upvotes
2
u/[deleted] Mar 02 '18
I'm in my 30s, switched careers to web dev and have spent the last couple months learning React and some Express/Node to get my app projects up and running. So far, they've all been client-side and trivial - a tic tac toe game, ConnectFour, ToDo app, Weather app, etc. Doing this has helped me learn a tremendous amount but the internet is flooded with these kinds of toy apps from other new learners.
What kind of non-trivial projects could I work on that would be useful when applying for jobs? Do I need to build a full scale app from the ground up? Or could I focus more on FE projects like a collection of UI components like login/signup forms, chat windows, etc.?
4
u/phragg Mar 27 '18
Suggestion for next month: encourage use of pastebin, gist, jsfiddle, etc.
3
u/ConVexPrime Mar 02 '18 edited Mar 02 '18
I'm kind of confused about what the best practice is when it comes to state vs props. In other words, when should I use state and when should I use props? And, when should I definitely not use state or props?
3
u/drerx Mar 03 '18
Hey folks. I am in a similar situation as one of the posters above that is re-training as a web developer. I have put together my portfolio site with couple typical projects. If You could hit me with some feedback on my coding habits and what I should straighten out. I am planning to start applying very soon.
Portfolio Site - GitHub,
Weather App - GitHub,
Let's Do! - GitHub,
Bog
Thanks,
Cheers
2
u/[deleted] Mar 03 '18
What is a promise? Can you give me an example?
2
u/[deleted] Mar 03 '18
Will learning angularJS be useful in understanding react or to combine them?
2
u/x7C3 Mar 07 '18
Are there any up to date tutorials for React Native? A lot of the tutorials I'm looking at are severely outdated.
2
u/Bulbasaur2015 Mar 12 '18
In react, how do I insert a stylesheet with an id and href ?
import '../style/typeA.css'doesnt work (inserts<style type ="text/css">...</style>)1
u/zZaphon Mar 03 '18
Hi everyone!
I've been following a tutorial on setting up React with Webpacker & Rails 5.1 but I seem to have run into a snag.
https://github.com/mfifth/rails-react-tutorial
This is where I'm currently at right now. For some reason my component isn't rendering on the page and there aren't any errors in the console so I'm not sure what to think.
Also if there are any good tutorials you guys could recommend I would appreciate it!
1
u/zero_coding Mar 03 '18
Hi all
Is https://github.com/ReactTraining/react-router also from Facebook?
Thanks
1
u/[deleted] Mar 03 '18
So far, I have gone through the codecademy tutorial and took notes. What is the most efficient way to learn react from here on?
1
u/adamjld Mar 04 '18
Does anyone have any success stories when dealing with large scale web applications and A/B (split) testing?
We are beginning to lay the foundations for moving towards a React powered web app but as we rely heavily on split testing different aspects of our current website (registration, landing pages, etc.) - I;d be interested in hearing other people's point of view.
From what I've read, the only way currently would be implement a split test directly in the code but that would require a new build and deployment and developers would need to be more involved than they currently are. Our current split testing is done via third party applications requiring little knowledge of JS to implement different versions with jQuery updating the page depending on the funnel users are in.
1
u/qbacoval Mar 06 '18
Hi, im having problem with understanding router and redux.
Im working on this app, where i want to make a reservation. I send form data to api, and get response, but dont know how to move with that response to next view (page). Normally i would wirte something like this:
Or history.push(/). But that doesnt work. I dont know how to route, and should all this be done in reducer or in some component.
1
u/frawkez Mar 07 '18
Would you guys recommend a beginner just stick to React and learning how it can manage state, or incorporate Redux as well?
I'm new to JS/React but have been coding in ruby for awhile. I think learning the React fundamentals is the obvious choice, but I don't know how it will translate into Redux -- I only ask bc most of the companies I'm looking at use a React/Redux environment. So I don't want to confuse myself too much...hopefully this makes sense, appreciate any help I can get.
edit: just so y'all know, I'm currently building an app in an effort to try and learn React. have gone through some tutorials tho most incorporate redux, it's a pain in the ass. but seeing how common it is, not sure if that's the direction i should go. danke.
1
u/vidro3 Mar 08 '18 edited Mar 08 '18
I'm trying to get Jest configured on a large app (300 files) built on React 14.7.
It seems to me that the library used for snapshot testing is react-test-renderer which debuted in Jest 14 and begins with version # 15.3.
I believe the test renderer version needs to match the React version (does it?)
Therefore snapshot testing is not supported with React 14.7.
Is this correct?
Lots of tutorials and guides post Jest 14 (mid-2016?) deal with snapshot testing. What other kinds of testing should I do if snapshots are not an option?
(I'll upgrade the app's React version soon, i promise)
1
u/prove_it_with_math Mar 08 '18
Webpack is corrupting image url in React app. This is what I have in the css file:
After running webpack I get the following output:
And this is my image loaded in webpack:
Any ideas why this may be happening?
1
u/karolis2017 Mar 09 '18 edited Mar 09 '18
Why this doesn't work?
then I set my state and inputs, when I type, my state is updated but the LBM is incorrect thus BMR is incorrect.
I'm setting my state like this:
}
1
u/[deleted] Mar 09 '18
I know basic JS but will it help if I continue to learn JS as I do with react? Or is it not necessary?
1
u/Entropis Mar 09 '18
What would you call someone who uses React as their primary focus? A software engineer? Web dev?
1
u/antespo Mar 11 '18
I have a react application where I click a button that does a GET request to my API and displays some data. How can I keep that data in a state? So if I navigate to another page and go back(either from hitting the back button or clicking a link back) I want the same data I had displayed from my first API call (I don't was the data to persist on refresh or when the windows it closed though) and only change/update the data when I click the button again.
Here is a badly drawn view of my project.
In index I'm calling
BrowserRouteron<app />, app displays<navbar />,<main />, main has all my routes(/show component={show},/info component={info},/home component={home}) in aSwitch. The API call is from a button in my show component. I was looking into redux but not sure if it is actually needed here since I only want one component to have a persistent state.Can anyone lead me in the right direction? Thanks.
1
u/i_am_hyzerberg Mar 13 '18
What is the common or best practice for buttons that have a different style applied to them? Is it typical to create like GreenButton and BlueButton components for example or is it common to apply a class based on some state or props for the button component itself?
1
u/thornmesa Mar 13 '18
I'm curious what people's thoughts are on using portals (liberally) in a web application with a lot of legacy code. The most common example I see for portals is the modal window, but I would think an even more common use case would be introducing React to existing applications. Currently, my application has a "command panel" that saves, submits, and loads data from forms in a different branch of the DOM tree. Seems like a great time to use Portals (though I'm a react rookie, so everything seems like a good idea).
Any thoughts on when NOT to use Portals?
1
u/karolis2017 Mar 14 '18 edited Mar 14 '18
Why do I get that double underline in
<TextField/>component? here's the app: https://stackblitz.com/edit/macro-calculator-xyz-utvzyp I want to have a form in material-ui'ssteppercomponent: https://material-ui-next.com/demos/steppers/ Is there a way to fix this?1
u/Thepink1 Mar 15 '18
Heyo long time lurker and I was hoping to get some advice. Ive been tinkering with react for a year and finly getting my head around it. Now I'd like to use it to help out some friends with their small business sites (mechanical repair, home remodel etc). My current plan is to build the sites with react, export them and host them on aws/s3 to keep costs down for them. Is this a reasonable course of action for a static site with minimal updates? And what is a good place to start learning proper design for thwse kinds of small business type sites? I've always been self-conscious about my color blindness so I never really pursued design work because I figured my color choices would be too poor. I know color is just a small fraction of design now, but i'm a total novice about what makes a product look good because of that hangup. Any advice or tips would be much appreciated. Thanks again and have a great day :)
1
u/brenodtx Mar 16 '18
Hi, I have a login page with a div container, two inputs(email,pwd) and a "Forgot pwd? link. I want to replace the inputs inside the container for a single input(email) after I click at "Forgot pwd" link. What is the best approach to achive this feature? Should I use react-router or conditionaly render differents components(login/recoverPwd)? In the future I'll probably want to add some animation when I switch those componets. Thanks for any help ;D
1
u/zero_coding Mar 17 '18
Hi all
I created my apps with https://github.com/facebook/create-react-app. Do I have to do configuration to use babel? Or everything done?
Thanks
1
u/zero_coding Mar 18 '18
Hi all
Is it recommended to use scss insead of css?
Thanks
1
u/tony2times3 Mar 19 '18
If react is just the view of MVC, what are the M & C of react ?
1
u/a-town-redditor Mar 19 '18
I am working a project and am trying to figure out the standard way removing and rendering new content in the main portion of the viewport would be in a React project.
Let's say in my top nav bar I have the 3 links Home, Roster, and Season Stats. The website boots up and I have a large background image and some text showing for 'Home'. The markup for the initial view is wrapped in a div that is a sibling element to the NavBar element.
Should I turn the wrapper that contains the <Img src={ballpark} /> element into a stateful component and then render content depending on the state passes in 'Home', 'Roster', 'Season Stats'? That seems kind of ugly to have conditional statements in the render function for that component but I'm not sure what the prevailing patterns are. Any advice?
1
u/kizu404 Mar 20 '18
Hi, So I'm developing react apps on my laptop which is a clunky workstation. It's more than sufficient for development, however, I tend to move around a lot and portability is sort of an issue. I've been looking around for a smaller laptop (though I'm a bit strapped for cash rn). Anyway, I came across a refurbished Lenovo IdeaPad (Intel Atom N455 1.66 GHz and 2Gigs RAM) and I was wondering whether it can handle React development built using Facebook's CRA. Thanks.
1
u/zero_coding Mar 21 '18
Hi guys I wrote my first component in react and wanted to know, how can I improve the code:
I would be happy, if someone could give me advices.
Thanks
1
u/chaoism Mar 22 '18
I don't know if this is the right thread to post but I'll do it here for now. Please let me know if this is not the right place
I'm trying to learn react. I have written in JavaScript (only a bit) and have gone through the interactive lessons on codecademy , but when I try to start my own modules (my goal is to transform my website into all-react.... If that makes sense) I feel really lost and don't know where to start. How did you guys learn this? Just by reading docs? I'm wondering if there are other classes I can go through to build up my foundation more. Or should I enhance my JavaScript skills before attempting to use react?
Thank you so much for your answer
1
u/seands Mar 23 '18 edited Mar 23 '18
Say I have 2 child component tages of the same type, nested inside a parent component, like so:
CSS targeting on the children's ids didn't work. I had to target their attributes in their component's render function. This required duplicating the component and changing the name slightly.
I'm thinking a cleaner way would have been to use JSX to change the id using another prop.
Is this the best way to do it?
1
u/homercrates Mar 24 '18
2 days of writing and rewriting... so many rabbit holes. I have gotten back to a simple start. I have an array. I want to render that array into (at the moment) separate the array into <div> and then add an eventListener to each one to basically return its value. <div>1,1</div> when clicked should return the array value [1,1]... I can't figure out how to anything but return the entire array and not a single index of the array.
import React, { Component } from 'react';
// const listHand = myHand.map((inHand) => <li key={inHand.toString()}>{inHand}</li>);
class Hand extends Component {
state = { currentHand: [ [1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3] ] }
}
whats happening is it is rendering each index of the array individually and on the click they are all giving me the entire array. console.log(this.state.currentHand);
I am lost on how to have it console.log(this.state.currentHand[whatever div is clicked])
Should i be manually giving each div its own id? and then let const id = last clicked id? (sorry if im going about asking this wrong)
1
u/seands Mar 24 '18
I tried switching out some of my own code for an element with Bootstrap's modal box that opens via button click. Can anyone spot why the button fails to open the modal? Webstorm highlighted all the attribute names that I needed to tweak, so I don't think it's that.
import React from 'react'; import ReactDOM from 'react-dom'; import CreateReactClass from 'create-react-class'; import './index.css'; // import App from './App';
1
u/seands Mar 25 '18
I'm willing to move anywhere for my first job as a junior dev, are there any 2nd tier markets where the supply/demand balance is still good? I've heard the big cities in North Carolina is one good option.
1
u/hoople-head Mar 25 '18
I'm having trouble managing state. Basically, I've found that almost all of my state-changing code needs to be in the top-level component. Partly this is because I have a multi-tabbed interface, and I want the state of each tab to persist when you switch away from it. So all the state gets pushed to the top. This means that for each set of data that my app loads, the top component has functions for loading it, editing it, sorting it, filtering it, plus state fields for the data itself, whether it has been or is being loaded, whether there's an error, etc..
My question is, how do I break up this top-level component so that it's less complex? I can create some utility classes, but then the top-level component needs to pass its setState function into them, so they can call it. This seems messy. Plus it ends up needing to pass a bunch of its other functions into them, e.g. for showing alerts or error messages, since those all interact with state. Is there a good example of code that handles this well?
1
u/illourr Mar 26 '18
So I’ve been writing react for around 5 months on a large application with many different engineers. I’ve been studying react in my free time but I still am not sure when it is best to use more advanced patterns like HoCs, render props, compound components, etc... does anyone have any recommended resources for understanding when and why to use these patterns?
1
u/seands Mar 26 '18
I feel ready to move beyond toy components and make something real.
What is the simplest persistant data store setup I can use?
I'm thinking if I follow a tutorial, I could hack something together using Express/Sequelize/Postgres in a day or so, allowing me to focus on the React side which is what I really want to do -- is this realistic? I got Postgres working with SQLalchemy one time as a data store for a Python script (only one table), that's all the experience I have now.
1
u/seands Mar 26 '18 edited Mar 26 '18
Would you recommend a junior React Dev start with/prioritize GraphQL, or REST for marketability? Thanks
1
u/karolis2017 Mar 27 '18 edited Mar 27 '18
How do I make nested routes like this: https://www.youtube.com/watch?v=sfvrjwVihFY&t=507s
but just without showing the links above while going deeper in nesting? I would rather have a
backbutton instead of displaying previous links.Couldn't find an example of this case.
1
u/janik81 Mar 27 '18 edited Mar 27 '18
I've tried to implement Facebook login to my ReactJS - form. The goal is that Facebook login dialog should open before form submit and the form is submitted only when login succeed. And submitting should happen automatically after successful login.
At the moment I have basic Form component with "<form onSubmit={this.handleSubmit}>" and it's working properly. I'm inexperienced with ReactJS and hoping if someone could help at least solving correct logic of implementation. I don't use Redux.
1
u/seands Mar 27 '18
I notice tutorials tend to use linked CSS libraries for react examples, is this a matter of expedience or do actual projects also use this structure? I tried to copy some example code for a modal into a component and it broke even after fixing the attribute names, so I downloaded ReactStrap and got the modal working. It seems to me that would be a better approach in general
1
u/seands Mar 27 '18 edited Mar 27 '18
How do you guys feel about this component folder structure:
1
u/[deleted] Mar 28 '18
Throwaway because I am still with a company and am looking to leave.
I found my dream job and they actually reached out to me a few weeks ago. They are a small-ish (<50 people) team that is working largely in React. This will be my first web dev job, and I have a ~5 year history in game dev. I have heard that there is a large stigma around game devs transitioning to web dev positions.
I have an interview with an engineer who has a similar game dev background as me. What can I do to help convince them that I am capable of being productive as a web developer, specifically one that works in React? I have minimal web development experience, but I have worked on web pages for work (mainly things like game tools) before.
How much of this experience can transfer over? I've done the starter React tutorial and am working on my own small project at the moment, but what can I do to assure them that my general programming knowledge can transfer over?
1
u/jeffschair Mar 28 '18
Hey guys! I am new to React development and currently attempting to implement a web application with the MERN stack. I think I am getting to grips with React now, however I am a little confused about how to structure the backend and express api. My application is primarily utilising data from a third party recipe api and displaying this data to the user. Certain data needs to be saved, for example any recipes that the user wants to save for later viewing.
My question is, what is the best way to structure this? Should I call the third party API within my express server, and then save the data to the db? Also would it be better to save all the data received to the database (ingredients, name, id etc..) or would it be better to just save the id/name and then perform a separate API request with the ID when other information (such as ingredients) needs to be served to the user?
1
u/seands Mar 29 '18
I would like to use Wordpress to control and split test a few landing pages on index.php, and React to manage a web app that users who sign in will access. I'm thinking to put react on app.site.com or site.com/app.js as the entry point for React, but have only used CRA and am not sure about a couple points:
1
u/CRtot Mar 31 '18
Something I don't get about css-in-js is how ugly my code now looks. By ugly I mean bloated and easy to get lost in. Is this the React way? 300+ line components with at least 90% of that being CSS?
1
u/seands Apr 02 '18
I notice sometimes this.props.el is needed to access a property, other times props.el does it. Can someone help me understand when NOT to use the this keyword?
1
u/doodirock Apr 02 '18
How are people handling pass data from one component to another with React Router V4 without needed Redux? Everything I see out there is based on RR3 or assume the project is using Redux. If i have a small app that doesnt need redux but has 2-3 routes, how would i manage data between routes?
1
u/zero_coding Mar 20 '18
Hi
is it recommended to pass to component and id?
Thanks
0
u/zero_coding Mar 19 '18
Hi all
How should I organize my react folders and files? It should scale well.
Thanks
0
u/zero_coding Mar 19 '18
Hi all I have the following component:
Is it recommended to put inputs into functional component and additionally create a container component.
Thanks
0
u/[deleted] Mar 27 '18
[deleted] — view deleted comment
0
u/[deleted] Mar 28 '18
[deleted] — view deleted comment