r/reactjs • u/acemarke • Sep 11 '17
Beginner's Thread / Easy Questions (week of 2017-09-11)
Looks like the last thread stayed open for quite a while, and had plenty of questions. Time for a new thread!
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.
21
Upvotes
6
u/Seoul_BMO Sep 12 '17
I just finished a basic react tutorial and am trying to build my own site and get hired. Can anyone point me to a good tutorial? I'm familiar with the MEAN stack.
3
u/alebrozzo Sep 12 '17
How small (or big) should a component be? Eg I have a page where you can see a list of people and an add button, the list has a number of columns for large screens and fewer columns for small screens. Also at the top of the screen there is a button that opens a pop up Window to choose the filters. Right now we have a component for the pop up screen, and another for the list and buttons. Is the main one too big? Another screen has the data for creating a user and a list of events associated, should that be two components or just one? None would be reusable.
3
u/Duffman- Sep 13 '17
Hi,
I will be on a holiday next week and I would like to catch up with react+redux as I have no knowledge on this topic whatsoever. I am currently building my UIs using plain old JS/jQuery/bootstrap. Could you guys point me to a good book that introduces react+redux? Unfortunately, I have no eReader so the book that's promoted on fullstackreact.com is not an option. Or is it that good I should buy it and print it on A4 before leaving for my trip?
Thanks!
2
u/givemepockets Sep 12 '17
Familiarizing with the redux pattern. This is such a 'duh' question, but can a patient soul explain to me how this isn't mutating state?
http://redux.js.org/#the-gist
The counter function returns
state + 1(in the case ofINCREMENTaction type). I simply don't understand how this isn't mutating and returning the initially provided state. In other more complicated examples I see a lot of use ofObject.assignto return new object copies, which makes sense.1
u/TooManyFrameworks Sep 16 '17 edited Sep 16 '17
I'm creating a MERN(including redux) app. I can't get hot reloading to work for react components.
In webpack I added these plugins:
In my server.js I've added:
Problem: How do I get hot reloading to work with react components? Am I missing a module or something?
1
u/notjonathanfrakes Sep 20 '17
So I'm a rails dev looking to punch up my UIs with React. And tend to learn from doing. Anyone have a good recommended tutorial (that isn't on the front page of Google)? If it's designed to work with RoR, even better.
1
u/ticktalker Oct 03 '17
Hey, everyone. Just got a new job and I'm currently learning how to write sagas. But I'm still not sure how to structure my sagas and I can't seem to find any examples that match my needs. Let's say I have a resource like a book and I need to make API requests for getting all books that get shown in a list and updating a book that is currently selected. I can't seem to figure out how to call these sagas in my root saga for books. I can't use a basic book call and then run sagas according to the action which is the approach I've seen in some github comments (lead dev has his rules and doesn't want to help). What I don't understand is how my root saga is run. I get the fork or all methods, but what if my methods aren't in order or don't have a rule to their calls. Update book can be called 20 times. I feel like I'm missing an article or example somewhere to clear this up for me. Any help would be appreciated.
1
u/ghuroo1 Oct 06 '17
Hey everyone,
I'm considering React is at v16 already, are learning resources from 2014, 2015, 2016 and 2017 still useful for me, as a complete beginner?
Did the framework change too much? Or should I stick only to recent tutorials/online lessons.
Thanks!
3
u/woollyhood Sep 19 '17
I'm using react-leaflet and I want to render components on top of the map. Problem is, the map is rendered on top of everything else. I've tried setting the z-index. Leaflet has built in components like layer controls and markers that render on top, but I can't figure out how to create a custom component on top, thanks.
2
u/bloody_hamster Sep 11 '17
What is the best way to store a session token inside a react app using redux?
2
u/wavematt Sep 12 '17
How does one typically handle content management with a single-page app built with React? I'm currently building a SPA that will feature a simple blog, project portfolio and contact form plus a couple of other static pages. I'm just curious if it is common to create your own content management solution separate from a React SPA or if there is some other solutions available out there.
2
u/EverAccelerating Sep 17 '17
I have an app with a component) displays some cost data for some products. The API call is dependent on three variables (all props to the component): Product ID, Period ('1-month', '3-month', etc), and Start of Period (epochtime)
I'm using Redux (with Thunk & Axios), but I'm unsure what the best way to store and retrieve the data. The way I'm doing it now is creating a unique ID based on the three variables above. So it looks like:
In my component, I create that ID and use it for my action:
this.props.fetchCostData(costDataId)And in
fetchCostData(), I deconstruct that ID, make the API call (via Thunk / axios), and in my reducer, I store it as that ID. Each entry in the state has three keys:data,isLoading&error.In my component, I basically have to check if
this.props.costData[costDataId](the Redux state bound to the component) exists and then check if it's loading or has an error before displaying the data.I feel like that's not a very clean way of doing things. Is there a better way of using Redux to store all my data?
2
u/iamfahimreza Sep 20 '17
I'm a PHP/WordPress dev and I interested to learn to react and react native. Please give me some suggestion, please.
2
u/stack_pusher Oct 02 '17
What do you think would be the best approach for static sections of text in your app? For instance, a couple of static text help articles here and there.
Getting tired of using es6
tick symbols?for string concats as my go to for escaping 'apostraphes'.Also going to be building out a blog feature that will be serving a lot of static content. So I am willing to put in a reasonable amount of effort for a solid solution.
Pretty much every react component I have made until now has been very input based. Not sure best approaches for serving static content.
2
u/sumdumbum19 Oct 20 '17 edited Oct 20 '17
Hi, I actually posted my question on /r/css, but thought this sub might be a good place as well.
https://www.reddit.com/r/css/comments/77naqn/loading_one_global_css_file_in_createreactapp/
Essentially, I currently have an app running off of Create-React-App (CRA) and modified my package.json to output .css files from my .scss files using their recommendation (https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-a-css-preprocessor-sass-less-etc). My problem, however, is that I use BEM and prefer a single global css file while developing. The way they have the app is one .css for each component I make in react. This causes multiple .css files for http requests and multiple rules cancelling each other out since I have a shared .scss file for utilities and other sharable files.
Is there something I can do (prefer not ejecting) like changing my package.json to have all css consolidate into one global.css file in development? I say specifically, development, since building allows webpack to minify my css into one global file (what I want).
fyi, here's my package.json file:
See here for an example: https://imgur.com/a/Midhs
1
u/unfeatheredOne Sep 13 '17
In my app I implemented a module that get's latidude and longitude of my current position and now I want to export this data to googlemap components somehow so the default position would be overwritten by my current one.
Here is my code structure: location.js
My googlemap file:
And finally my app.js file (the important bits):
I know I have to somehow export the state between parent and then children, but I got really confused there on bigger project and got completely lost when working with 3rd party libs. Any other solution how to get user location for googlemaps that is beginner friendly is also appreciated, im not limited to those libraries, but thats the only ones i could find and get to work, just cant make them work together
1
u/[deleted] Sep 13 '17
I have a parent component that controls all state and a child component that returns a dropdown menu and passes (is supposed to pass, rather) the result of clicking an option up to the parent component and update the parent's state.
Parent:
// We're controlling all of our state here and using children // components only to return lists and handle AJAX calls.
Child:
import React from 'react'; import PropTypes from 'prop-types';
When I open the app as is and choose an option from the dropdown menu, two things don't happen that I would expect to happen. One is that the second menu (the one returned by StopList, the code for which isn't included here) is not populated with any data. The second thing is that 'setting state' is not logged to the console. That second thing is leading me to believe that somewhere in my code, I am not correctly passing the option clicked in the dropdown up to my handleSubSelect method and therefore not correctly setting any new state.
1
u/sardinhas Sep 14 '17
Hi!
Is there any way of displaying a random image from my assets folder?
Basically I have a folder with images and I want to display a random one every time the user refreshes the page. Do I need to know the name of the images or can this be made dynamically? I know I can do this by naming my images "1.png, 2.png, etc" and using Math.random(), but considering I will want to add to this folder and use this type of logic in another part of the application, I wanted to stay away from that approach.
Sorry if this doesn't fit here or it should go to a broader subreddit (/r/askprogramming maybe?), but since I am actually using React I thought I could ask here :D
Cheers and thanks!
1
u/NV43 Sep 14 '17
With React Router, how would I set the current pages navbar active class when the user is brought directly to a page via a URL?
1
u/[deleted] Sep 17 '17 edited Sep 17 '17
<MyComponent id="myId" />1
u/[deleted] Sep 17 '17
What is a good grid system (not using new css grid) for react that works without touching
webpack.config.js?1
u/[deleted] Sep 18 '17
How do components in libraries handle styling? How do you make it easy to style a component you didn't write yourself?
1
u/[deleted] Sep 18 '17
How to insert html entity inside render() in jsx?
1
u/SenecaWithLasers Sep 18 '17
Does someone know how to make webpack resolve.alias works in tests?
Thanks!
1
u/aakber Sep 18 '17
Is it possible to declare constants in separate file and import this constant in component and use it in constructor?
1
u/redcomet120 Sep 25 '17
I'm using react router 4 and made this auth HOC based on the React Router 4 auth section https://gist.github.com/Redcomet120/00801d5f8f1d07d973cbeb7da2d21e89
but when I try to pass items to the child component as props I get the errors shown in the gist. AuthRoute works fine using component instead of render. Not sure what I'm doing wrong.
1
u/mmathrowaway16176017 Sep 26 '17
This is minor but it's been bothering me a lot:
Using this as a reference: https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en
When I use react dev tools on chrome why dont I see the component name in huge purple letters. That section is completely ommitted. I see everything else the state, props, keys, etc.
1
u/longnt80 Sep 27 '17
I just started learning Reactjs. Here's a practice project I just did involving calling more than 1 API. There seems to be a lag/glitch when clicking the button the change between API call.
I could pre-load both APIs at the beginning and swap them when user click. However, it only works with small data. If there's multiple APIs, it could slow the starting load time which is bad UX.
Please tell me how to improve this and remove the glitch/lag.
Thank you very much.
1
u/mcarocha Sep 28 '17
Hi! I just started learning Reactjs and I created a simple Chat component as a practice project.
I'm still very confused about how to organize components properly and how to improve reusability.
Please, take a look at my code and give me some tips about how to improve it!
Thank you!
1
u/epetkus Sep 28 '17
Hi there! I am full-stack Rails dev and I do want to bump up my React knowledge. I believe I have a decent JS fundamentals (I also worked a bit with Ember.js). I know myself that the best learning for me is following "real-world app" example type of tutorial (this is how I got into rails tbf - by creating famous twitter clone). Is there anything like it in react world? E.g. creating full admin dashboard, basic crm or anything like that type of app? thanks!
1
u/tyorke Sep 29 '17
Just got started with react-bootstrap and have had success using the <NavDropdown> component but am running into a situation where my menu item list is growing too large and am in need of creating a mega menu. Having some issues figuring this out, could someone point me in the right direction?
I'd like something similar to the menus found here https://bootsnipp.com/snippets/featured/bootstrap-mega-menu
1
u/BobM93 Sep 29 '17
I'm now building my first website with react, and was wondering if I could set the className of a component outside the component?
For example, I have a "contact" section that I want displayed in the header on desktop, and in the footer on mobile. So, in the footer, I have:
Which doesn't seem to work.. I thought this property might only be accessed in the component itself so I could add it with ${ this.props.className } in Contact.js, but then I would need to add this every time I want to add a class to a component, which frankly, doesn't seem like a good thing to do.
Is there another way to do this? Or is it perhaps bad practice to add classes from outside the component?
1
u/AvoidingMyself Oct 02 '17 edited Oct 02 '17
Does anyone get really lost learning from "follow-along" tutorials(text/video)? After following along I forget 80% of whats said and I have a shallow understanding of why they did xyz.
Are there different approaches you would suggest?
1
u/[deleted] Oct 03 '17
Hey guys! Just wanted to ask what was the easiest backend to set up with React Native so I can store images and upload them from the camera roll? Thank you!
1
u/EverAccelerating Oct 04 '17
I'm trying to find a library for menu dropdowns, with the key being that the dropdown should be able to render outside the DOM flow because the menu itself would be hidden if it's actually attached to the trigger element itself.
To further explain my use case, I have a fixed header at the top of my app, and I want to show a menu when clicking on an icon in the header. Since the menu is a
position:fixedelement that's 60px high, the menu will have to live outside the header (unless there's a way to show content beyond 60px?I briefly tried out react-dd-menu, but this did not work for me.
Are there any others that will suit my purpose?
1
u/janderssen Oct 09 '17
Hey all,
Just wondering what is the recommended ReactJS to display a dialog to the user (I am currently use react-bootstrap and wrapping the Modal inside of a component). The reason I ask is I have kind of done two different kind of implementations, and they both seem a little complex for my liking, and I am sure there is a better way again.
I was originally using the BootstrapDialog, which is very jquery way of working, and was also unable to get unit tests working with it via Jest and ReactJS, so I investigate the react-bootstrap way, and wrote my own dialogs manually.
Thanks in advance for your help. Cheers
1
u/[deleted] Oct 09 '17
Just got introduced to reduxSaga, having a hard time understanding how to write and execute one. would appreciate if some one provides me a good resource.
1
u/Troppelmann Oct 09 '17
Hey guys,
I just recently started working with react and I am currently working on a login. What I dont seem to get is the Session thing that is usually available in PHP. I know that there are states that can be set but are there any kind of "global" states that I can refer to. Like a field for email and username. I think what I am looking for is a substitute of the old Session variables in PHP.
And also, is that even the correct way to handle these things in React? How do you guys handle session variables?
Looking forward to some input! Cheers!
1
u/deadcoder0904 Oct 14 '17
Hey yo, so I want to create a React component & publish it to
npmso that I can use it in my next projects. The component has some methods which require to use Component State. My guess is I need HOC but not sure. So how to create it ??P.S: Its a form component for React Native as there are not many for RN & most of them don't give that much flexibility. So I created my own but I don't know how to push it to
npm. I have simple libraries published onnpmbut not a React Component which uses Component State.1
u/AvoidingMyself Oct 14 '17
I'm making a MERN(inc Redux) app. It's a crud app with login/signup. I'm probably keeping /server and /client in the same root directory.I'm confused on these topics:
1
u/IAmWhoISayImNot Oct 16 '17
Hey guys. I'm semi stuck with a design concept. In my app I'm using react-redux. My 'root' component, app.js, is where I load all the data. That works perfectly. My problem is this, I have a page where you go to add a new post. This page references the the store in redux and when app.js gets the data, mapstatetopros loads it and passes it to the component. Is there a call back or something that I can listen for that will I form me that the props have changed as that's where I filter my data.
1
u/theirongiant74 Oct 16 '17
If I'm calling a component function from render are there any positives/negatives to passing the data as parameters over letting the function gather them from state/props as needed.
This is a very trivial example but is there any reason to do
rather than
I'm fairly new to testing (just started unit test, not yet tackled testing react components) and I'm wondering if there is any advantage to not having the function using this.props. The first way seems a little more explicit to me.
1
u/RezoNation8 Oct 17 '17
Hey guys!
So I'm building a simple VM that compiles and executes assembly language. This execution then updates memory and register displays that I have built. I was hoping to incorporate play, pause buttons and step buttons so that the user can go through instructions at their own rate to better visualize the method. However, I am rather new to javascript and React and don't know how to incorporate the two together. In Java, a language I am more comfortable with, I would use multithreading but this is clearly not an option. Are there obvious ways that I could move through instructions on a staggered rate that can be paused and continued? Kind of similar to a stopwatch. If you feel inclined to help feel free to DM me.
Thanks!
1
u/stupidthrowaway12671 Oct 17 '17
If my component is stateless & doesnt use lifecycle methods, but it has functions such as
onSubmit = () => this.props.submit()should the component be a class or stateless functional component?
1
u/mj_mohit Oct 18 '17
i am building a new fullstack App. have previous exposure with backbone and express. For some reasons i have skipped everything after that in JavaScript world. So far things which i have decided -
React for V.
No Flux/Redux. I'll stick to MVC style, my developers come from php world and it's a very heavy database focused app with lots of external APIs, crawlers etc. Traditional models are a must (atleast on server). CSS Modules with SCSS for styling.
webpack for bundling.
express router on server and history for client. React-Router feels counter intuitive to me. (need a lot of magic for SEO friendly urls - everyone understands static routing)
I need server side rendering.
I will worry about data-modeling and controllers later. as of now express routes gets data from mongoose directly and can send that as JSON. from the same routes I want to send HTML (rendered with react) to client on initial page load, from where React can take over with history router and request for fetching JSON data from client.
As of now i am struggling with how to structure react components and how to setup webpack (where to put entry file, which loaders etc)
I am not sure if above makes any sense. Please ask if it doesn't.
Is there any example of such a setup or boilerplate. Again following is the stack i want to use-
express
mongoose/sequalize (i will write a backbone like model later - if you can suggest any lib to model data)
routers can work as controller as of now (very rudimentary backbone like approach)
No redux
No react-router
React for V both on server and client
webpack
CSS Modules
http://react-toolbox.com
1
u/jackwilsdon Oct 22 '17
Is it a good idea to put my "selected filters" state in Redux? I feel like it's not information required by the rest of the app, so should it be in there?
The filter is used on the products page to filter the list of products being shown on the page, I'm currently storing this state within the page component.
1
u/jackwilsdon Oct 23 '17
Should I be converting my flat normalized state into a deep nested state before passing it into my presentational components, or would it be better if they accepted a flat state? It requires more "complex" processing in the component to handle a flat state, but is it a better idea?
The data is related in such a way that I'd still need to pass some normalized data in, alongside the un-normalized data.
1
u/EverAccelerating Oct 27 '17
Quick question that may not be exactly React-specific.
I'm using Axios with Redux / Redux-Saga. On a given page, it may be possible that I'm starting up 50+ axios requests concurrently (and independently), all to the same domain. I know the browser limits to 6-10 simultaneous connections (correct?). So does that mean I can rely on the browser to handle making sure only that many of my axios connections are actually running at a given time, or do I have to throttle on my end too and make sure only X number of Axios processes are running?
1
u/CanIhazCooKIenOw Oct 27 '17
Redux related question.
I have some settings I get from an API, that I'm storing in a store. I'll use them in one section but on another section I only need to display a resume of that data.
Say the settings are:
And the resume of it is
Does it make sense to store the resume in it's own store of just use selectors on render ?
I've implemented using selector but I think I'm having extra renders because of a new object being passed every time.
I guess the correct option to avoid extra renders would be to store it but because I give the possibility to update those settings I'll have to update the store in different places every time that happens.
What would be the correct/logical approach to this ?
0
u/[deleted] Sep 18 '17
[deleted] β view deleted comment
0
u/[deleted] Oct 09 '17
[deleted] β view deleted comment