r/reactjs • u/gaearon React core team • Jul 25 '17
Beginner's Thread / Easy Questions (week of 2017-07-24)
A bit late, the 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.
8
Upvotes
1
u/jawdroppingjack Jul 25 '17
Is it considered bad practice to pass in jsx elements to a parent elements props with an array? I know I'm not using the children.
1
u/El_Rista1993 Jul 28 '17
Can someone help me figure out a bug that's driving me nuts?
Basically here's the code:
And here's output:
3
u/kittymeteors Aug 04 '17
I tend to write a lot of code that looks like this:
Basically, the render function should wait for soms async content (based on the props) and then render the component.
Is there a better way of doing this?
2
u/hlw_rocer Jul 31 '17
Hi, completely new to React and had a question about routing (haven't really looked at react-router yet, so sorry if there's an obvious answer to the question).
Right now, I more or less understand how everything works, but I'm struggling to understand how to route everything. I'm using express, react, and socket to create a real time web app, and at the moment, have express handling the request to ('/') by returning index.html.
To my understanding, by calling reactDOM.render(component, webelement) at the bottom of a component js file, will render that component and put it in the corresponding webelement within index.html. (So for example if index.html had <div id='app'>, reactDOM.render(component, document.findElementById("app")) will render the corresponding HTML). As an example, let's say I return <div> Hello </div>
Now, say I had a second page. website.com/page2, and a second component c2. If I had a link pointing to that page on the index page, to my understanding React will update the DOM if we attach the second components render method (this sounds really wrong, and worded poorly so I think I'm misunderstanding something). Here, I update <div id = 'app'> to contain World.
But now, my issue is what if I directly navigate to the page website.com/page2? Will react render Hello onto it since its directly called in the component?
I think my issue here is that I don't fully understand how to incorporate routing in express with routing in React. I've tried to look it up and found things like passing arguments into the html templates when rendering with express (Using reactDOMserver.RenderToString()), but that seems unnecessary. Is there a way to tie specific components to each page independently?
Thanks!
2
u/monkeyt6 Aug 03 '17 edited Aug 08 '17
Hi, I am new to react and currently I am trying to create a basic react app and then style it. For that, I've come across two libraries, styled-components and glamorous, and both look equally good. What are the differences between these two in their working? Which one would be lighter and preferred for big applications?
1
u/Threeshoe Jul 26 '17
I'm not sure if this is a React problem or a general problem with webdev but I can't get this function to work on Android chrome. It's bound to onKeyUp but I've tried onKeyPress and onKeyDown as well. If anyone has advice I'd really appreciate it. You can try it here: https://ttches.github.io/hang-man/
github: https://github.com/ttches/hang-man/tree/master/src
1
u/invismann Jul 25 '17
Hello! What I'm trying to do is rework my react-router so the NavLink renders a completely new page on click, instead of rendering at the bottom of the div, as shown in this gif.
Here's the content of my main App.js component:
The Home component's code, which holds the NavBar that's used in the Home Page.
I'd appreciate any help with this thanks!
1
u/humblespace Jul 26 '17
theoretically, is it possible to replace index.html with index.php? (create-react-app) I wanted to make head section dynamic
1
u/[deleted] Jul 26 '17
Does anyone know how to update the CSS of an element when the state is a specific value?
1
u/El_Rista1993 Jul 27 '17
Where should I post about testing errors? I'm running Karma/Enzyme/Sinon and when I attempt to shallow render a HOC (a "pure" version that is unconnected to my redux store) and when I attempt to do a .find("classname") I am getting an assertion error with "expected { Object (root, unrendered, ...) }" instead of a failed test and I don't understand what is happening.
1
u/magneticmagnum Jul 27 '17
I'm at a company where I'm slowly implementing React into our monolithic frontend. All views and routes are required to be rendered with JSPs and Java Spring.
How I'm implementing React is to bundle the react.js files and then include them in my JSPs.
Currently all my JSPs are returning business logic gathered from our backend to render Headers, Footers, authentication, and a script to the bundle.js for that page.
With this setup, is it possible to get hot loading working?
1
u/[deleted] Jul 27 '17
Ok: moron here. Please be patient
So, I'm not a developer, I'm a maths teacher. I've had an idea to do a website in JS to help other people teach (http://www.ticktockmaths.co.uk/Number/FactorsandMultiples/difffactmult.php , but with more topics) and to help me learn to code. Using JS (not that I was any good at it!) it was a bit fiddly. I saw react and it seemed perfect for what I wanted to do. A single page web app. Brill.
I've completed some code academy topics on it, however I'm really struggling to access any of the tutorials on the web and get started with trying stuff out because what the hell is node.js and npm and whaaaaatt......All this stuff seems way more complicated than simply using a cdn as I've done before.
Is react workable with a CDN? If people are doing small projects surely this is the best way to get started? Everything I read asks me to do some terminal stuff to set up my project. Is there a reason for this?
1
u/knowssome Jul 31 '17
Why should I use Link/NavLink from React Router v4 when anchor tags work just fine. I understand that NavLink gives extra functionality when creating site navigation but Link and NavLink are throwing cryptic errors in a small project I'm working on even though anchor tags to the specified routes work just fine, and I can easily create the functionality I need from that.
1
u/india_resists Aug 01 '17
Hi! Newbie here, can someone please explain or point me to the link to let me know the reason/use case why we use
this.props.children. And also the proper way to implement it. Thanks.1
u/loonylovegood Aug 01 '17
What would be a good next step (starter kit, boilerplate, scaffolder etc)after getting the hang of create-react-app?
1
u/rdevilx Aug 03 '17
I'm currently working on a project and I'm fine with the most part because I made my own Boilerplate (Big Mistake) about that my mistake was that I forgot to incorporate Hot Reloading. It's a project using React - Redux. I just want to enable HRM in my project for React and Redux for state preserving and reloading of React Components.
P.S.: I'm on Webpack V2, not V3 (I'll migrate to it soon) P.P.S.: I've seen a lot of tutorials about the same but I end up breaking the project.
1
u/kucukkanat Aug 04 '17
I have a survey of 5-15 questions and I didn't want to create views for every single step and set routes with react router so I came up with this. Is there a better way of handling these ? There seems to be a lot of copy-paste and I want to reuse these props.
1
u/[deleted] Aug 04 '17
I have an Express web application, with a user interface made with Jade (Pug). I would like to re-make the user interface in React, what would be the easiest way to achieve this in an existing project? Or should I just use create react-app, and call the API endpoints from a new project?
1
u/FFRedshirt Aug 05 '17
Hi Guys,
I'm working in Optimizely on a site built on React and I need to change a link. I'm not familiar with React so I'm hoping a pro here can help me out.
In an external JS - how can I alter a button's destination without access to the core React app?
<a class="this-btn hide@m" role="navigation" href="/some/url" data-reactid="72">Some URL</a>
I am currently using JS to alter the href (don't think it makes a difference) and also doing a window.location.assign.
But when I do this - its linking to /some/url first... then it will load my final destination.
Is there a better way? Thanks all!
1
u/mathrowaway1768 Aug 07 '17 edited 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/GherriC Aug 07 '17
I'm fairly new react and redux and I'm don't quite understand how the this keyword works with higher order components. I was reading an article that re-implements a basic version of redux's connect function and I don't quite understand how this.props line works in the article's code.
I understand that the outer function accepts the two maps..ToProps arguments and returns a function which accepts a component as it's argument. The inner function returns a wrapper component with the same props as the argument component also with new props specific to redux. The thing I confused about is what is causing "this" to bound to the argument component.
0
u/[deleted] Jul 25 '17
[deleted] — view deleted comment
1
u/[deleted] Jul 29 '17
[removed] — view removed comment