r/reactjs • u/gaearon React core team • Jul 17 '17
Beginner's Thread / Easy Questions (week of 2017-07-17)
Our weekly Q&A thread starts!
The previous one was here.
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.
12
Upvotes
5
u/Zaszen Jul 19 '17
Why Facebook docs and many guide advice to not overuse Refs ?
https://facebook.github.io/react/docs/refs-and-the-dom.html
Is there a situation where you should never use refs ?
1
u/[deleted] Jul 20 '17
Hey all, newbie here.
I have a Form component where I have a multi-step system going. I also have a progress bar located in my Navbar component. These are not directly related to each other, as within my App component they are rendered at the same time.
However, if I want my progress bar to...well...progress then my Navbar component needs to be aware of the current state of the Form component. Would this be where something like Redux would come into my app? I haven't ventured to Redux yet being a newbie but I think this situation requires it, would I be correct in saying?
1
u/Ob101010 Jul 20 '17 edited Jul 20 '17
Regarding deployment, a.k.a. beyond just learning and a local environment.
So webpack spits out a bundle.js and I have a index.html that brings it in. I put these on a server, point Nginx.conf at index.html... and it works.
So, why are people using ExpressJS? Im trying to understand the server side requirements of serving a production ready react site once your app is complete.
1
u/Ob101010 Jul 20 '17
Another newb question (sorry if Im flooding this place...)
Since my app is essentially all bundled up in bundle.js, is there any reason to worry about someone downloading that file and looking for exploits and/or stealing my work?
Like say I make an app that gets insanely popular. Whats to keep anyone from downloading bundle.js and copying my site onto their own domain?
1
u/Leezorq Jul 25 '17
I have a question about styling the structurural parts.
Let's say I have a dashboard with 3 columns. Each column holds different component.
Now... where do I put the styles for the layout e.g:
Do I put it inside my component? But what if i want to use it in a different type of layout?
Or do I put it on a wrapping div something like this:
Or should I create a reusable <Container /> and <Col /> components for structural stuff? Thanks!
3
u/evsoul Jul 17 '17 edited Jul 17 '17
I feel like I am a new regular in this thread, but I am making progress :) I am building a weather app for my portfolio and I am stuck on updating the component state with whatever city/zip code the user puts in the input field. I have a default state set to Los Angeles and have a console.log that runs with every render showing the current state of "city", but when I type in a new city the console.log still shows Los Angeles, and the weather on my app doesn't update to the new city. I know it works on the API level because if I change the city in my code and save it, the app updates properly. So I am certain that I am handling the
setStateimproperly.Removed the long code, here's the Gist for it for better readability: https://gist.github.com/gsarpy/30d7491cfec52d8128e0593ee9c893f0
I feel like it's something very rookie that I am missing here, but after endless hours of reading documentation, watching state change tutorials, and changing my code, I have not found a solution. Please let me know if you need more info from me. I appreciate the help if anyone has the time and patience!
2
u/MarceloLopezUru Jul 20 '17
Great initiative :) I'm doing some research about React, since my dev team is starting to develop more and more with this library. What were the main challenges you faced when starting to code with React?
2
u/_CassandraWasRight_ Jul 22 '17 edited Jul 22 '17
Is there an overview of the major React forks? I'm mulling a few projects, but I'm having trouble collating the mountains of information.
What are the use cases for the following:
Did I miss any big ones?
And then there are tightly related projects such as:
Then there are others...
And surely hundreds more. It's pretty bewildering for a newbie.
So to back up to a very high level... For example, to build a personal finance utility app with web updates but no multi-user functionality that needs to run on Windows, MacOS, and mobile... what flavor fork should I go with? Or what fork for a 3D massively multi-player game app on web + mobile? For a single-player puzzle game on every platform possible? A data-intensive enterprise business critical app on Windows, Mac, and Linux with a 50,000 user base?
Thanks -- I did a lot of app development back in the day, and my how things have changed! javascript was always one of my favorites, and I'm looking forward to learning new stuff.
1
u/[deleted] Jul 17 '17 edited Jul 17 '17
Hi - Learning react, not sure if this is a beginner question or not, but here I go.
I have a structure like this:
So, I want to be able to open and close the modal in <Panel> but I don't want to do this by changing the Panel state, because I have some charts in <PanelBody> that get redrawn when they don't need to be.
I found this post about how to trigger a method in a child from the parent, and this might work but the solution just seems ugly: https://stackoverflow.com/questions/37949981/call-child-method-from-parent
That linked solution also implies that <PanelModal> is going to manage its own state. state = {show: bool} which would trigger the Bootstrap modal to show or hide.
I've been working with the show state in the <Panel> itself, but like I said, changing that state forces a redraw of the other parts of the panel, which I don't want.
I've looked into shouldComponentUpdate but by the time I've got nextProps and nextState, the Panel state.show has already updated to whatever it is toggled at at that moment.
So, what is the pattern to follow in cases like this, or what am I doing wrong.
Understanding state has been the biggest challenge so far, not that it is hard to grasp, but it is hard to know when a component, like a Modal, should or should not manage its own state. So far in the docs it seems like the idea is to always move state up, and I'm fine with that, but I don't want the whole app to redraw just because <App> state has changed with either show or hide modal. Similar to inputs. The state updates on every key change, which isn't ideal in all cases.
Thanks,
1
u/Danack Jul 18 '17
I've been following the Reddit example from http://redux.js.org/docs/advanced/ExampleRedditAPI.html and tweaking it so that I can add other stuff alongside it.
One thing I found, is that one of the functions is coupled strongly to the layout of the state in the store.
That function assumes that the reddit info is at the top level of the state, which of course broken when I moved the reddit info down a level, to be at
state.reddit_stuffso that I could havestate.my_stuffalongside it.Is that standard good practice to have functions be directly coupled to the state structure? Wouldn't it be better to have all of the information needed from the state to be mapped .....somehow, somewhere else so that updating the structure of the store doesn't require changes to call-back functions across the application?
Any clues as to what that better practice might be?
1
u/wntrm Jul 19 '17
What would be a viable way to improve performance of rendering 100+ <tr> elements beside paging? I was thinking of lazy loading but the libraries I've found so far are not something I can use inside <table> because they make use of <div>. Is there any lazy loading technique with plain react?
1
u/shushotora Jul 19 '17
Is there a way to use css modules and allow css importing, so I don't need to set every className throughout my application?
1
u/[deleted] Jul 19 '17
I am looking to create dynamically loading content to display various projects of an architecture firm using React. I will be taking content from a database to load them onto a template page where only the content change depending on the link clicked. (Single page with different content based on what was click) Where do I start? Is there a guide somewhere? I know html, css, and javascript. I appreciate any help at all!
1
u/fuzzyluke Jul 22 '17 edited Jul 22 '17
Hello, I've got a question regarding react-route-redux.
My Links:
My Route:
My MainContent component:
Only the first link I click actually loads up the content, the second link will not work at all.
I don't know how to dispatch() outside of componentWillMount() in this case, any help would be appreciated.
Note: i'm still a newbie at this!
Added note: I tried componentWillUpdate and componentDidUpdate, but I'm getting too much recursion warnings :|
1
u/shdq Jul 24 '17
Hey! I have <Board /> component with state and <InputForm /> component with <Input /> extracted from it. What is proper way to "lifting state up" from <Input /> to <Board /> throw <InputForm /> to change state? It works fine with state in InputForm with this code in InputForm:
and this code in Input:
Or may be there is should be another approach. This problem occurred when I started to extracting components as docs recommend. Thank you!
1
u/theGuacIsExtraSir Jul 25 '17
Hi all,
I'm having some problems with my ReactJS + Firebase app. I'm sorry if this isn't the correct place.
I am trying to set loggedIn to true if the user is signed in. The end goal being that if they are logged in, I replace the nav component "signUpAndLogin" with "logOut"
Everything is fine in the constructor, but it seems that when I try to see the this.props.loggedIn value in my render method, it looks at the value before it even gets set. If I put a timeout function to delay the console.log in my render, it shows the correct this.state.loggedIn value, but without it it just always says false.
Any help would be absolutely amazing. I've been stuck on this for 2 days now.
}
1
u/gekko27 Jul 25 '17
Hi, I have a relatively high level architectural question. Long time js dev, relative react noob, absolute redux virgin.
I want to create a simple app with a list of 3 users, and show each user's top 5 favourite songs. Due to the way the back end is set up (which I don't control), the actual API response to get the favourite songs only returns a song ID and a time stamp - I then need to make a separate API call to get the metadata about each song.
Normally I'd just use promise chains to wait until all the data was there and then render the whole view. However, I've been trying to learn the "right" way of doing it and have now got myself ball-deep in sagas, generators, redux reducers etc.
Before I rage quit and move all my Ajax calls back into component lifecycle methods, is there a better way to grok this? The whole flow is pretty alien to me.
0
u/[deleted] Jul 18 '17
[deleted] — view deleted comment
1
u/[deleted] Jul 19 '17
[removed] — view removed comment