r/reactjs • u/gaearon React core team • Jul 11 '17
Beginner's Thread / Easy Questions (week of 2017-07-10)
A bit late, a new weekly Q&A thread for you!
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.
7
Upvotes
2
u/mathrowaway1768 Jul 15 '17
How should I be testing? I hear about stuff like Mocha. What are good sources to learn about it?
2
u/webdevop Jul 12 '17
How do I rewrite this in React?
I'm just beginning to learn to react and I would like to understand how the component structure would look like in the case of a simple application like the one above.
I have an array of elements which I rotate through ever second, and depending upon the elements value, I change the className for a few elements.
I want to know if
#viswould be one component that will maintain its state or will every.lampbe a component having their own states. Incase of the latter, how will I tell my script that only a few lamps should have their state set and not all of them.React and ReactDOM are already added to the pen so you should be able to write React code (including JSX) in the same pen.
1
u/evsoul Jul 14 '17 edited Jul 14 '17
Can someone ELI5
bind(this)for me? My understanding ofbind(this)is when you pass a method or function into a child component as a prop, you need to bind it at the parent component's level so the child component knows that when that method/function is used, the actual method/function lives in the parent component and not the child component itself. Is this the correct understanding of bind? edit: are there any other scenarios that you would use bind aside from functions/methods?1
u/poopootoad Jul 16 '17
Learning React by Kirupa Chinnathambi has been a good book so far, but he tells me something that appears not to be true with my version of React (15.6.1):
For reference, this is the example code—Learning React uses Babel in browser for the most part but I couldn't get it to work in Firefox so I just skipped right ahead to transpiling (if it matters):
I did not get the warning he talked about in the Console or on the React tab from the Firefox add-on. Has anything important changed about component keys?
2
u/klarcgarbler Jul 12 '17
I just started a new create-react-app project and see it now has a service worker. Is it really only telling the browser to cache files? Isn't that overkill for a service worker? I thought they were more for heavy background tasks. Wasn't a cache manifest enough?
2
u/[deleted] Jul 13 '17
What sort of architecture would you use if you were making a simple component to strap onto an existing Wordpress site?
I'm trying to make a small calendar widget which grabs data from an API and displays tooltips to the side. It's more or less just for practice, but I want to do it right, the "functional programming" way.
2
u/ggilberth Jul 13 '17
Is it okay to make components that are just HTML elements, but our specifically styled ones? Like H1's etc, using perhaps styled components?
1
u/shearbrilliance Jul 12 '17
I have a question regarding props.
Lets say I have a userdetail page with a userroles component. If i understand correctly i have to pass all the required props from the userdetail component to the userroles component.
I want to have a userdetails component with only userdetail props and then have a userrole component with a userid prop. When componentWillMount() fires the actioncreater.getUserRoles with the userId prop will populate the userrole component.
I use an interface for the incoming props in my userrole component so if i only pas the userid i get an error that the actionCreator.getUserRoles prop is also needed.
I also find it strange that if i create a route then i dont have to pass al the props. In that case I can use props.match.params.userId to get the userId.
My question is: Do I really have to define all the props in the top component and then pass them to the child component(s) ?
*Sorry for bad grammar, English is not my native language.
1
u/Ob101010 Jul 12 '17
I dont get how state isnt mangled by the reducers in the example ToDo app.
https://github.com/reactjs/redux/blob/master/docs/basics/ExampleTodoList.md
Specifically, compare these 2 reducers :
And
So, in todos.js, were returning an array, the first element is the total of the state object, the second element is an object with (I assume) the new state? These get glued together, I think, to represent the new state.
Later, an action of type 'SET_VISIBILITY_FILTER' triggers the other reducer to modify the state by simply returning action.filter. WTH?!? Why dosent this mangle the state object all to heck? Is the state object like a que where it pushes stuff on to it? If so, where does the object added by todos go??
So confused lol.
1
u/jokingsuyo Jul 15 '17 edited Jul 15 '17
I'm used to writing a React component in the following format.
Compared the code below, it seems like you don't need to bind the function. And you don't need to use
this.state. Can anyone explain why the code below works as well?1
u/[deleted] Jul 16 '17
I'm in a situation where I have a
Homecomponent that makes an API call for images. Upon success I route to aGallerycomponent and pass the data along usingthis.props.history.push('/gallery', [data]). In addition to the API data, I also pass along other props that I want the Gallery to know about like what the user entered in the form. So my second argument to thepushlooks like thisFor some of the form inputs, they are optional, so when I display them, I want there to be a default value if they didn't enter anything. Should I be doing this in the
Homecomponent? So just check before sending off thepushif one of theformInputis empty and adding in a default? Or should I be doing this in theGallerycomponent? Note thatHomeandGalleryare not nested components, so this is why I'm usingthis.props.history.pushto transition between them.1
u/mathrowaway1768 Jul 16 '17 edited Jul 16 '17
Making a Minesweeper app w/ 4 files: Minesweeper.js, Board.js, Row.js, Square.js.
I want right clicks to flag a square as a mine. I'm passing {onContextMenu=this.handleFlagClick} to Row & Square. How do I pass the clicked square as a parameter without losing the event object?
My attempt: In my Square.js I include
Is it bad practice to bind the parameter like this?
0
u/[deleted] Jul 16 '17
[deleted] — view deleted comment
-1
u/[deleted] Jul 12 '17
[removed] — view removed comment