r/reactjs • u/gaearon React core team • Aug 07 '17
Beginner's Thread / Easy Questions (week of 2017-08-07)
Woah, the last thread stayed open for two weeks! Sorry about that :-) This one may also stay a big longer than a week since Iβm going on a vacation soon.
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.
27
Upvotes
4
u/GherriC Aug 08 '17
Hi all, I posted similar question in the previous beginner thread, but the thread got removed before I got an answer. I'm trying to understand some of the mechanics of how higher-order components worked. I looked at blog post where the author makes a simple version of redux's connect function and but I'm unsure about how the {...this.props} line works. Could someone check my understanding?
(Partial code from the article)
So my understanding right now is that {...this.props} is not determined until the the wrapper component is actually mounted. The wrapper component takes the entire prop object that the original component would have received if it was in the same position and spreads those props into the original component along with new props from the wrapper. The reason for using spread operator is used is because the makeup of the props object received will depend on the parent component passing down props and referring to specific props by name would make the wrapper less re-usable. Is this correct, or am I missing something?
3
u/LigerZer0 Sep 07 '17
So I am building an app and have decided to go with react because I want to learn it.
The app will be single-page. Basically just a search bar, and some dynamically loaded content below (images and text). Main features are:
My question is, as I am new to react, where should I start? I have gone through a couple of tutorials, but I feel like those apps are so different than what I wish to make that I still feel lost about how to even start mine.
For example, do I need to set up and populate the database separately, and then build the app on top of it, or is the react philosophy to generate the database via react?
Does anyone perhaps know of a similar app that is open source? Any help would be greatly appreciated.
Thank you.
2
u/mr_yip Aug 12 '17
I've just recently taken a few tutorials on reactjs and am now trying to get started on my own web app. I have experience working with frameworks like uikit and JS libraries like masonry but have no idea how to integrate these things into a react application.
Am I able to use 3rd party frameworks with react? What about JS libraries such as masonry or d3js? Are there any guides that can help me better understand how to use them?
Or am I over complicating it and these libraries work right out of the box?
2
u/EverAccelerating Aug 13 '17
I'm still a relative newbie to React. I've contributed to an existing project, but now I'm tasked with starting a new project from ground up. Of course, I'm using Create React App as a starting point, and that part is going well.
My confusion is regarding CSS. I'm a little confused as to the options available, and what would be ideal / needed. I know there's SASS and Less, but there's also CSS Modules. One of my questions is, is CSS Modules complementary to the first two, or a replacement for / competitor to?
The main things I want in my CSS are variables, mixins, auto-prefixing, CSS linting, and each React component having its own CSS namespace. So I want a system that covers all those (and possibly more). What would the recommendation here?
Second, I'm aware that if I do end up using CSS Modules, that I'll have to use eject on my React app, as detailed in this Medium article. From what I understand, with Create React App, you have a single dependency, and if you want to upgrade and keep up with the latest in React, you just have to update that one single dependency. So the question is, after ejection, in practical terms, how easy/difficult is it to upgrade and keep up with the latest versions? Are there compatibility issues that I'll run into that Create React App would solve for me? Do I lose out on other benefits of pre-ejection?
Thanks!
2
u/janderssen Aug 16 '17
Hi all,
I have been optimising the image loading in the browser, and would like to show how it originally worked, and the new optimised version (by the way, much much faster (ie instant)). I would like to know if the new way is ok from a ReactJS point of view :-)
First I would download the image as follows :
Then in the render I would have something similar too :
The biggest problem was the toDataURL was extremely slow for some images, so to remedy this I now put a div around the img tag with a ref.
The image on load now is changed too :
And the ref callback uses the image directly as follows : onImageDivRendered(imageDiv) {
Am I doing something very wrong, is this concept ok ? Thanks in advance for comments and suggestions.
2
u/deadcoder0904 Aug 17 '17
What is difference between Data & UI State ?
2
u/KFriedChicken Sep 07 '17
Hello!
Does anyone know where I can browse React example sites along with their source code?
2
u/MyCookieAndLatte Sep 09 '17
I have 2 routes. Users get to the second when clicking on a part of the first. I would like the first route to fade out before the second route appears. I've looked into react-transition-group but haven't been able to achieve this.
Any suggestion?
1
u/mathrowaway1768 Aug 07 '17
Tried deploying app to Heroku (using webpack):
I have a /src; /dist(which is .gitignored)
Am i supposed to
For 2) When I tried pushing to heroku I would get errors because i used babel-loader, css-loader, etc. Am I supposed to add these as dependencies for package.json or leave them as devDependencies?
Also which of the two methods is considered better or standard?
1
u/mathrowaway1768 Aug 09 '17
What is complex state? I see the word being used a lot but I cant find the definition.
1
u/[deleted] Aug 10 '17
Hiya! Hopefully this is a silly question. I'm new to react (Switching from Angular), and I'm having a hell of a problem with what seems like a reasonable thing to do.
Basically the layout of our application is this: A server hands the frontend a JSON blob that contains all the data to load a page (generally from a rendered Protobuf file), and the frontend react app eats that blob and generates the right components, etc. That part works great. However, the Protobuf we generate that data from also contains some constants and enums, which we refer to as bootstrap data. This is immutable data that is just used to help render the right information and make our lives a little easier.
Basically, I need the app to hit an API endpoint on the backend the first time the page loads, populate some variable somewhere, and then have that variable hang out in global scope. How should I go about this? I really don't think just passing it down the props chain makes much sense, and I really don't want to have some promise object I have to resolve everytime I need to use it. This application also has no mutable global state, so something like redux seems like crazy overkill. I played around with context, but that didn't quite seem to meet my needs, plus all the red text around it seems like a silly risk for such a small feature.
In angular, I would've just stored this data in a service, and all would be dandy. How do I accomplish this with react? It seems like I could just slap this data on the window object, but that feels like the wrong answer. Am I over thinking it, or is there a proper react way to address this problem?
1
u/janderssen Aug 10 '17
Wow, I wish I knew about this thread like 12 months ago :-) Anyway, I have been slowly trying to remove all of jQuery from my app, and change to the more "React" way of doing things, and been very successful so far. However, not so sure about this situation, but I am sure someone here knows how to fix it :-)
My render function has the lines as follows :
So when the user clicks on the button, I want to activate the file dialog box, so currently I do this as follows :
What is the way I should be doing this in ReactJS ?
Thanks in advance for your help.
1
u/perpetual_papercut Aug 12 '17 edited Aug 12 '17
Hey I've a question on componentWillReceiveProps
I have a text input with an onchange handler that passes it's value up to a parent component. The value of the text input comes from it's parent component as a prop named "term".
So, in the componentWillReceiveProps method I do the following:
const { term } = nextProps; this.setState({ term });
Is this bad practice?
here is an image of the code: http://imgur.com/XqqAyYQ
1
u/AdonisK Aug 13 '17 edited Aug 13 '17
I've been reading React code for a while now but never really developed anything. My question is related to passing and reading props in a parent-child Component relation. In some cases people use spread to pass all parent props to a child (or in case of an iteration, the Object keys-pairs of the current iterable) and in others they pass the props to the child explicitly.
When should each style of passing props be used and why?
1
u/wntrm Aug 15 '17
Anyone using recompose? How has it helped you write better/cleaner code?
1
u/evsoul Aug 15 '17
What is the best way to grab form input changes and update state without hard coding for each input name?
For example, I have a form with 10+ fields and I want to update state on the parent component whenever a user changes data in the field. I know how to use the
onChangeandsetStatebut how can I write one block of code to handle a potentially dynamically changing form?is this the best way?
or is there a better way?
1
u/irregular_regular Aug 16 '17
{pos} prints the Common Word properly but {parts_of_speech} prints out [object Object] instead of Common Word. How can I get the Common word wrapped in span to print out properly?
1
u/[deleted] Aug 16 '17 edited Aug 16 '17
I have a question about making a click event run the method I pass it. The below abbreviated code works:
However, originally I had tried to call the
handleClickmethod like this, and it didn't work:I was trying to follow an example from the book "Learning React" by Kirupa Chinnathambi that has
Now, what was wrong with my first attempt,
onClick={this.handleClick}? Is it just a ES5 vs ES6 thing? The author of the book goes on to talk about not being able to listen for events directly on components due to React's virtual DOM, and how you can work around that by passing events as props...or something. I really don't understand it. I've seen examples in other places where the method call is not inlined as in the working example above.Any clarification you guys can supply would be greatly appreciated. Thanks!
1
u/deadcoder0904 Aug 17 '17
Why to use normalizr & reselect with Redux ? I have read the docs & the examples are complicated to understand. Would be happy to get only beginner level examples π
1
u/Sitethief Aug 18 '17
I've started to use Redux in my learn/test app. I used to have my whole app in one component to test things, but I've started to split things in smaller components. Do I need to set connect mapStateToProps and mapDispatchToProps in every component separately?
I wanted to know this because my main component had a searchform that ultimately fetches data and shows this in a list component. And when I scroll I load more data in the list, but that happens in the list component, and thus I need to dispatch in that component I reckon?
1
u/DaniSenpai Aug 18 '17
What's the difference between this:
and this
Why does the first one work but the second one doens't?
1
u/DaniSenpai Aug 18 '17
As someone who's currently exploring the framework market for a new one to adopt, why React over Vue? Other than performance and job availability (that React has a lot more of) how does React feel over Vue?
1
u/Peng-Win Aug 18 '17
In componentWilLreceiveProps(), should each prop be changed separately inside an IF statement?
i.e.
as opposed to
1
u/ThornScythe Aug 22 '17
Hello I have a redirect problem with react router My main entry point of the App looks like this
On the first view I have a redirection
when I had the Route like this
it worked like a charm but now the props are being lost what I'm doing wrong?
1
u/FortuneBull Aug 23 '17
My application allows for information to be added, edited, or removed on click event on desktop but will not change on iOS devices. My gist is below with some slightly condensed code. Pretty much the important parts are the method onSubmit and the button with className "edit-course".
https://gist.github.com/dsopel94/830fd6f97f406e048b4524519fe352b0
1
u/EverAccelerating Aug 23 '17
Is there a React equivalent of EmberJS's
ember-cli-mirage, basically a server that serves mocks API responses?We're starting a UI project at the same time as the API backend, so we may not have the APIs ready by the time we need them on the UI side, hence the need for a mock server.
1
u/Peng-Win Aug 25 '17 edited Aug 25 '17
Should you wrap everything in try-catch to avoid crashes?EDIT: Never mind, stupid question
1
u/austintackaberry Aug 26 '17
Can someone please explain how
thisworks in the code below? I just started learning React, and I really want to understand the basics before moving on. I have been stuck for hours on this one.I have read the
thissection of YDKJS multiple times, but I'm fuzzy on classes, constructors, and what is actually going on whenFoois called by<Foo />.1
u/robertmdesmond Aug 27 '17
Where are demos of some example (open source) web apps built with React? a/k/a "starter" apps
1
u/robertmdesmond Aug 27 '17
What backend solutions are currently popular for hosting and data?
1
u/invismann Aug 27 '17
How exactly do I learn to do more with React? I have basic knowledge of React and React Router but I'm not entirely sure what I should be doing to make better single page apps.
1
u/Filo01 Aug 27 '17
just wondering if anyone could recommend some tutorials that implement API database with react and redux
also how do people feel about redux should I be implementing it into my webapp?
thank you in advance
1
u/irregular_regular Aug 27 '17
How can I convert
to not get
1
u/mynameiscody07 Aug 28 '17
Is this the correct way, or the way you would handle a component that may or may not have click handlers attached to them.
1
u/Sitethief Aug 28 '17 edited Aug 28 '17
I need some help with this input. If this.props.keyword is set it needs to be the value, if its empty then value should not be used and a placeholder should be used. The problem here is that value='' will be used if there's no keyword set, this, somehow, prevents the user from using the input field. But I can't find a good way to not set the value attribute when there is no keyword.
1
u/paulreggie Aug 29 '17
Having problems with a ListItem and Nested List with containerElement with a NavLink/Link from react-router and a rightIconButton. When clicking on the rightIconButton with nested list, the Link/NavLink is also triggered. I have not been able to figure out a work around to this.
<ListItem value={3} primaryText="My Collection" leftIcon={<Avatar src="http://i.imgur.com/fmvLZGS.png" />} initiallyOpen={false} containerElement={<NavLink to="/CollectionPage" />} nestedItems={[ <ListItem value={4} key={1} primaryText="Wines"/>, <ListItem value={5} key={2} primaryText="Arts"/>, <ListItem value={6} key={3} primaryText="Vehicles"/> ]} />
1
u/janderssen Aug 29 '17
Expert advice on alterating a large "configuration" structure using flux etc. So I have a structure with 10 different configuration options, so in the component I have a choice of either creating actions for updating each of them individually which sends events to the store and the store keeps an updated version until the user clicks save or cancel, and if a save action is executed the store would receive the event from the action to send the update to the server, or cancel if necessary. Now my question relates to the fact the the actions will have 10 verisons of the following :
Which I find very verbose, I think the easiest way is for the store to export manipulation functions that the component can access directly, but as I understand it, this is not looked upon in a good light.
So what is the solutions others have done to make this a nicer implementation, and keeping to the single truth concept.
Thanks in advance.
1
u/petar02 Aug 29 '17
Where can I find a tutorial that would teach me how to use redux with react.
Can you give me an idea about a project that I will have to use react and create REST API.
1
u/realchriswells Aug 29 '17
I'm looking to get started with React but after reading a post or several I'm seeing terms like state (local/global), one-way, redux and a bunch of other terms that fried my fragile little brain.
I'm a frontend developer looking to expand his horizons and get a new tool under his belt and having a play with the new hotness, but I have no idea where to start. I have written an "app" (following a tutorial) that will show a message on page load and change when a button is pressed.
I initially wanted to look at API handling with react, but I think I need a bit more back-to-basics experience with the library first.
Please help.
1
u/humbletales Aug 31 '17
Have a quick question about how React can speak to a backend. I understand how it can work with a full Express set up, but can you just call simple scripts?
I'm trying to rebuild an old app with React; the previous app was just a jQuery SPA that made ajax calls to a php script. Can I do something similar, just ajax to script.js rather than script.php? Or will there be no 'server' running unless I have Express?
1
u/evildonald Aug 31 '17
I have a navigation Header with the following structure.
const Header = () => (<header><nav><ul><li><NavLink to="/" activeClassName="link-active" exact="true">Home</NavLink></li></ul></nav></header>)I want to setup a background image that "cover" fills the Header.
the image is at a relative
./../img/splash.jpgin the samesrcfolder.I have searched and searched and I can't find an answer to this.
What is the best way to make a background image work here?
thanks in advance!
1
u/EverAccelerating Sep 02 '17
I'm just learning Redux right now, and I have probably an obvious question: does everything that can change about your app go into Redux? I mean, besides the obvious stuff like data from APIs, or things that happen / change because of user clicks.
Let's take a somewhat contrived example. In my app, there will be a div / component with its own scrollbar. When it is scrolled, I add a drop shadow to the top so it's obvious it's scrolled. So I have an event listener and I keep track of whether the drop shadow should be on or off. But nothing outside of that component needs to know about the scrolled position. So would this also go in Redux? Or is it okay to keep the state internal to that component?
I can probably come up with more examples of components that have a state that no other component -- parent or child -- should ever care about. So again, should Redux be tracking everything?
1
u/mikejones1477 Sep 03 '17
Has anyone ever created "property" configuration files for their react/react native apps based off environments?
I would like to make a property file that I can add to my .gitignore that has some local configuration settings. I can then reference that property file in my code and just commit and push normally to github.
1
u/Web_Fender Sep 06 '17 edited Sep 06 '17
I have a question in relation to build approach using React and how to show/hide specific child components.
I have a list of filters down the side of a search results set. Each filter change will update the result set via the onChange method.
There's a collection of about 10 different filters; these are for cars. Filters such as colour, transmission type, engine size etc. The site content editors have the ability to turn on and off each filter - which in turn determines what filter data gets returned via an API end point (JSON).
I'd like to render these filters programatically using React depending on what is returned via the JSON object. What would be the most suitable way of doing this?
Right now I'm thinking of having each filter as an individual child component that accepts props from the state at the top level. But what I don't understand is how I show/hide each filter (child component) depending on if it's actually needed (present in the state) or not.
I've had a skim over the 'conditional rendering' page on the official React docs - it details using inline operators and simply checking for the present of something in the state e.g. 'if state contains said filter - render the filter'. This makes sense to me - but having about 10 components - each wrapped in operator checking the state for all 10 seems like an anti pattern approach. Is there a better way to do this or am I simply over thinking things? If not - are there are performance implications to this approach?
TL:DR - what's the best way to render child components only if they're present in my top level state object? I don't always want include them on the page - only if they're present in my JSON object.
Thank you in advance.
Edit - I thought I should clarify. I'm speaking figuratively about components. It is in fact a single react app/component rendered on the page. Each filter would be a child component within the same application. Something like:
1
u/Sennon Sep 06 '17
Are
styled-componentsstyling all done at runtime? Or does it extract all the static styles before runtime and the dynamic values are during runtime?1
u/theirongiant74 Sep 07 '17
If I'm writing the frontend of a section of an existing web app at a fixed url am I better using react-router with memory history to deal with the different views or is there any compelling reason not to just write a top level view component that deals with this.
1
u/deadcoder0904 Sep 11 '17
How to know which components must be a HOC (Higher Order Component) ?
1
u/Findail Sep 11 '17
I have a form written in straight HTML, is there a simple process i can follow to convert it to React?
1
u/acemarke Sep 11 '17
We just opened up a new questions thread, as this one had been open for a while. If your question didn't get answered in this thread, try asking it again in the new thread!
1
u/mrchaieb Aug 16 '17
first, im new and want to learn programming i found a website with fun quizzes (arabic) http://quizzzat.net/index.php/quiz/view/1416 and i want to create website idea like it but i don't know what programming language needed to create this quizes . if someone can help i will appreciate that. im also very experienced Graphic Designer if someone can help me in the project and i help him with designs in exchange
0
u/[deleted] Aug 15 '17
[deleted] β view deleted comment
0
u/[deleted] Aug 24 '17
[deleted] β view deleted comment
0
u/[deleted] Aug 29 '17
[deleted] β view deleted comment
0
u/[deleted] Aug 31 '17
[deleted] β view deleted comment
0
u/[deleted] Sep 01 '17
[deleted] β view deleted comment