|
275 | 275 | |259| [Is it good to use arrow functions in render methods?](#is-it-good-to-use-arrow-functions-in-render-methods)|
|
276 | 276 | |260| [How to prevent a function from being called multiple times?](#how-to-prevent-a-function-from-being-called-multiple-times)|
|
277 | 277 | |261| [How JSX prevents Injection Attacks?](#how-jsx-prevents-injection-attacks)|
|
| 278 | +|262| [How do you update rendered elements?](#how-do-you-update-rendered-elements)| |
| 279 | +|263| [How do you say that props are read only?](#how-do-you-say-that-props-are-read-only)| |
| 280 | +|264| [How do you say that state updates are merged?](#how-do-you-say-that-state-updates-are-merged)| |
| 281 | +|265| [How do you pass arguments to an event handler?](#how-do-you-pass-arguments-to-an-event-handler)| |
| 282 | +|266| [How to prevent component from rendering?](#how-to-prevent-component-from-rendering)| |
| 283 | +|267| [What are the conditions to safely use the index as a key?](#What-are-the-conditions-to-safely-use-the-index-as-a-key)| |
| 284 | +|268| [Is it keys should be globally unique?](#is-it-keys-should-be-globally-unique?)| |
278 | 285 |
|
279 | 286 | ## Core React
|
280 | 287 |
|
|
415 | 422 |
|
416 | 423 | 
|
417 | 424 |
|
| 425 | + State is similar to props, but it is private and fully controlled by the component. i.e, It is not accessible to any component other than the one that owns and sets it. |
| 426 | +
|
418 | 427 | 9. ### What are props in React?
|
419 | 428 |
|
420 | 429 | *Props* are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.
|
|
4351 | 4360 | const element = <h1>{name}</h1>;
|
4352 | 4361 | ```
|
4353 | 4362 | This way you can prevent XSS(Cross-site-scripting) attacks in the application.
|
4354 |
| -262. ### How to update rendered elements? |
| 4363 | +262. ### How do you update rendered elements? |
4355 | 4364 | You can update UI(represented by rendered element) by passing the newly created element to ReactDOM's render method. For example, lets take a ticking clock example, where it updates the time by calling render method multiple times,
|
4356 | 4365 | ```javascript
|
4357 | 4366 | function tick() {
|
|
4366 | 4375 |
|
4367 | 4376 | setInterval(tick, 1000);
|
4368 | 4377 | ```
|
4369 |
| -263. ### |
| 4378 | +263. ### How do you say that props are read only? |
| 4379 | + When you declare a component as a function or a class, it must never modify its own props. Let us take a below capital function, |
| 4380 | + ```javascript |
| 4381 | + function capital(amount, interest) { |
| 4382 | + return amount + interest; |
| 4383 | + } |
| 4384 | + ``` |
| 4385 | + The above function is called “pure” because it does not attempt to change their inputs, and always return the same result for the same inputs. Hence, React has a single rule saying "All React components must act like pure functions with respect to their props." |
| 4386 | +264. ### How do you say that state updates are merged? |
| 4387 | + When you call setState() in the component, React merges the object you provide into the current state. For example, let us take a facebook user with posts and comments details as state variables, |
| 4388 | + ```javascript |
| 4389 | + constructor(props) { |
| 4390 | + super(props); |
| 4391 | + this.state = { |
| 4392 | + posts: [], |
| 4393 | + comments: [] |
| 4394 | + }; |
| 4395 | + } |
| 4396 | + ``` |
| 4397 | + Now you can update them independently with separate setState() calls as below, |
| 4398 | + ```javascript |
| 4399 | + componentDidMount() { |
| 4400 | + fetchPosts().then(response => { |
| 4401 | + this.setState({ |
| 4402 | + posts: response.posts |
| 4403 | + }); |
| 4404 | + }); |
| 4405 | + |
| 4406 | + fetchComments().then(response => { |
| 4407 | + this.setState({ |
| 4408 | + comments: response.comments |
| 4409 | + }); |
| 4410 | + }); |
| 4411 | + } |
| 4412 | + ``` |
| 4413 | + As mentioned in the above code snippets, this.setState({comments}) updates only comments variable without modifying or replacing posts variable. |
| 4414 | +265. ### How do you pass arguments to an event handler? |
| 4415 | + During iterations or loops, it is common to pass an extra parameter to an event handler. This can be achieved through arrow functions or bind method. Let us take an example of user details updated in a grid, |
| 4416 | + ```javascript |
| 4417 | + <button onClick={(e) => this.updateUser(userId, e)}>Update User details</button> |
| 4418 | + <button onClick={this.updateUser.bind(this, userId)}>Update User details</button> |
| 4419 | + ``` |
| 4420 | + In both the approaches, the synthetic argument e is passed as a second argument. You need to pass it explicitly for arrow functions and it forwarded automatically for bind method. |
| 4421 | +266. ### How to prevent component from rendering? |
| 4422 | + You can prevent component from rendering by returning null based on specific condition. This way it can conditionally render component. |
| 4423 | + ```javascript |
| 4424 | + function Greeting(props) { |
| 4425 | + if (!props.loggedIn) { |
| 4426 | + return null; |
| 4427 | + } |
| 4428 | + |
| 4429 | + return ( |
| 4430 | + <div className="greeting"> |
| 4431 | + welcome, {props.name} |
| 4432 | + </div> |
| 4433 | + ); |
| 4434 | + } |
| 4435 | + ``` |
| 4436 | + ```javascript |
| 4437 | + class User extends React.Component { |
| 4438 | + constructor(props) { |
| 4439 | + super(props); |
| 4440 | + this.state = {loggedIn: false, name: 'John'}; |
| 4441 | + } |
4370 | 4442 |
|
| 4443 | + render() { |
| 4444 | + return ( |
| 4445 | + <div> |
| 4446 | + //Prevent component render if it is not loggedIn |
| 4447 | + <Greeting loggedIn={this.state.loggedIn} /> |
| 4448 | + <UserDetails name={this.state.name}> |
| 4449 | + </div> |
| 4450 | + ); |
| 4451 | + } |
| 4452 | + ``` |
| 4453 | + In the above example, the greeting component skips its rendering section by applying condition and returning null value. |
| 4454 | +267. ### What are the conditions to safely use the index as a key? |
| 4455 | + There are three conditions to make sure, it is safe use the index as a key. |
| 4456 | + 1. The list and items are static– they are not computed and do not change |
| 4457 | + 2. The items in the list have no ids |
| 4458 | + 3. The list is never reordered or filtered. |
| 4459 | +
|
| 4460 | +268. ### Is it keys should be globally unique? |
| 4461 | + Keys used within arrays should be unique among their siblings but they don’t need to be globally unique. i.e, You can use the same keys withtwo different arrays. For example, the below book component uses two arrays with different arrays, |
| 4462 | + ```javascript |
| 4463 | + function Book(props) { |
| 4464 | + const index = ( |
| 4465 | + <ul> |
| 4466 | + {props.pages.map((page) => |
| 4467 | + <li key={page.id}> |
| 4468 | + {page.title} |
| 4469 | + </li> |
| 4470 | + )} |
| 4471 | + </ul> |
| 4472 | + ); |
| 4473 | + const content = props.pages.map((page) => |
| 4474 | + <div key={page.id}> |
| 4475 | + <h3>{page.title}</h3> |
| 4476 | + <p>{page.content}</p> |
| 4477 | + <p>{page.pageNumber}</p> |
| 4478 | + </div> |
| 4479 | + ); |
| 4480 | + return ( |
| 4481 | + <div> |
| 4482 | + {index} |
| 4483 | + <hr /> |
| 4484 | + {content} |
| 4485 | + </div> |
| 4486 | + ); |
| 4487 | + } |
| 4488 | + ``` |
0 commit comments