concept prop in category react

This is an excerpt from Manning's book React Hooks in Action.
But updating state and re-rendering is not a one-off task. A visitor using your app is likely to cause a multitude of state changes and React will need to repeatedly ask your components for the latest UI that represents the latest state values. It’s your components’ job to convert their state and props (the properties passed to them) into a description of their user interface. React then takes those UI descriptions and schedules updates to the DOM (the browser’s Document Object Model) where necessary. At various points this book uses cycle diagrams and accompanying step-by-step descriptions to highlight the repeating nature of these updates, as shown in the Cycle Diagrams section on pages mm and nn.
function MyComponent ( props ) { // Maybe work with the props in some way. // Return the UI incorporating prop values. }
Up to this point, we’ve seen how components can manage their own state with the useState, useReducer and useRef hooks and load state data with the useEffect hook. It’s common, however, for components to work together, using shared state values to generate their UI. Each component may have a whole hierarchy of descendant components nested within it, chirping and chirruping to be fed data, so state values may need to reach deep down into the descendant depths. In this chapter we investigate concepts and methods for deciding how to manage the availability of state values for child components that need to consume them, by lifting state to common parents. In chapter 9 we’ll see how and when React’s Context API can be used to make values available directly to components that need them. Here, we stick to using props to pass state down to children.
We start, in section 7.1, with a new
Colorscomponent that shares a selected color with three child components. We see how to update the shared state, managed by the parent, from a child. The rest of the chapter uses the bookings app example to explore two approaches to sharing state: passing a state object and a dispatch function for a reducer to the children and passing a single state value and its updater function to the children. Both approaches are common patterns and help to highlight some common questions regarding state, props, effects and dependencies.
In the previous version, when we called
useReducerfrom within theBookablesListcomponent and assigned the dispatch function to thedispatchvariable, React knew that the identity of the dispatch function would never change, so it didn’t need to be declared as a dependency for the first effect. Now that a parent component passesdispatchin as a prop,BookablesListdoesn’t know where it comes from so can’t be sure it won’t change. Leavingdispatchout of the dependency array prompts a warning in the browser console like the one in figure 7.12.Figure 7.12 React warns us when dispatch is missing from the dependency array.
![]()
Including
dispatchin the dependency array is good practice here; we know it won’t change (at least for now), so the effect won’t run unnecessarily. (Later in the book, we look at using the useCallback hook to maintain a consistent identity for functions passed in as props.)

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
Listing 2.6. Featured component—src/components/featured.jsx
import React from 'react'; const Featured = (props) => { const buildIngredients = (ingredients) => { #1 const list = []; ingredients.forEach((ingredient, index) => { list.push( <li className="item" key={`${ingredient}-${index}`}> {ingredient} </li> ); }); return list; } const buildSteps = (steps) => { #2 const list = []; steps.forEach((step, index) => { list.push( <li className="item" key={`${step}-${index}`}> {step} </li> ); }); return list; } return ( <div className="featured ui container segment six wide column"> <div className="ui large image"> <img src={`http://localhost:3000/assets/${props.thumbnail}`} /> </div> #3 <h3>{props.title}</h3> #3 <div className="meta"> Cook Time: {props.cookTime} </div> #3 <div className="meta"> Difficulty: {props.difficulty} </div> #3 <div className="meta"> Servings: {props.servings} </div> #3 <div className="meta"> Tags: {props.labels.join(', ')} </div> #3 <h4>Ingredients</h4> <div className="ui bulleted list"> {buildIngredients(props.ingredients)} #4 </div> <h4>Steps</h4> <div className="ui ordered list"> {buildSteps(props.steps)} #5 </div> </div> ); } Featured.defaultProps = { #6 labels: [], ingredients: [], steps: [] } export default Featured; #1 Function converts an array of ingredients into an array of list items; this is called from the render function. #2 Function takes in the steps array and converts it to a list item, called from render function. #3 Featured component is a container that renders information about the featured recipe; it renders a recipe passed in via props. #4 Function converts an array of ingredients into an array of list items; this is called from the render function. #5 Function takes in the steps array and converts it to a list item, called from render function. #6 Set properties that are arrays to defaults so if there’s no data, the component can still render. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":3,\"ch\":66},{\"line\":3,\"ch\":68}],[{\"line\":71,\"ch\":0},{\"line\":71,\"ch\":2}],[{\"line\":18,\"ch\":66},{\"line\":18,\"ch\":68}],[{\"line\":72,\"ch\":0},{\"line\":72,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":37,\"ch\":65},{\"line\":37,\"ch\":67}],[{\"line\":38,\"ch\":65},{\"line\":38,\"ch\":67}],[{\"line\":41,\"ch\":65},{\"line\":41,\"ch\":67}],[{\"line\":44,\"ch\":65},{\"line\":44,\"ch\":67}],[{\"line\":47,\"ch\":65},{\"line\":47,\"ch\":67}],[{\"line\":50,\"ch\":65},{\"line\":50,\"ch\":67}],[{\"line\":73,\"ch\":0},{\"line\":73,\"ch\":2}],[{\"line\":53,\"ch\":65},{\"line\":53,\"ch\":67}],[{\"line\":74,\"ch\":0},{\"line\":74,\"ch\":2}],[{\"line\":57,\"ch\":65},{\"line\":57,\"ch\":67}],[{\"line\":75,\"ch\":0},{\"line\":75,\"ch\":2}],[{\"line\":63,\"ch\":65},{\"line\":63,\"ch\":67}],[{\"line\":76,\"ch\":0},{\"line\":76,\"ch\":2}]]"} !@%STYLE%@!
To write a story, you add code to the components/stories folder. There’s a file for each chapter section that uses stories. Storybook provides two functions: storiesOf and add. The first function, storiesOf, adds a new story to Storybook, which shows on the left (Chapter examples and Todo app). The section function (add) adds a specific example to the story (link displays with props, list item functional). To see this in action, review the following code:
Listing 3.8. Test the listItem done prop—components/stories/chapter_3_4.js
storiesOf('Chapter 3.4 examples', module) .add('JSX basic button', () => ()) .add('link displays with props', () => ()) .add('list item functional', () => ()) .add('list item with done prop', () => ( #1 <ListItemFunctional completeCallback={actions.complete} deleteCallback={actions.delete} name="Cleanup mess" done="true"/> #2 )) .add('list container with list item', () =>()); #1 Add a story with the add() function—pass in a title for the story and a function that returns the component you want to render. #2 Add done property—component checks for truthy value here, so you can set it to true. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":2},{\"line\":4,\"ch\":70}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":59}],[{\"line\":6,\"ch\":24},{\"line\":6,\"ch\":55}],[{\"line\":7,\"ch\":24},{\"line\":7,\"ch\":43}],[{\"line\":8,\"ch\":24},{\"line\":8,\"ch\":70}],[{\"line\":1,\"ch\":34},{\"line\":1,\"ch\":36}],[{\"line\":2,\"ch\":42},{\"line\":2,\"ch\":44}],[{\"line\":3,\"ch\":38},{\"line\":3,\"ch\":40}],[{\"line\":9,\"ch\":2},{\"line\":9,\"ch\":4}],[{\"line\":10,\"ch\":46},{\"line\":10,\"ch\":48}]]"} !@%STYLE%@!
React relies on props for communication between components.

This is an excerpt from Manning's book React in Action.
When should you use state and how do you get started using it? For now, the simple answer is when you want to make changes to data stored within a component. I said props were immutable (not modifiable), so if you need to change data, you need mutable state. In React, data that tends to need to be mutable often comes from or is the result of user input (often text, files, toggled options, and so on), but could be many other things. To keep track of user interactions with form elements, you need to provide an initial state and then change that state over time. You can use the constructor of the component to set the initial state for your component, a comment box component that builds on the ideas and concepts from earlier code listings. It will allow you to add comments to a post via a simple form. The following listing shows how to set up the component and set initial state.
We’ve talked about how React lets you work with data in a mutable way via state and setState, but what about immutable data in React? In React, props are the primary way to pass immutable data. Any component can receive props (not just those that inherit from React.Component) and use them in their constructor, render, and lifecycle methods.
Props in React are more or less immutable. You can use libraries and other tools to emulate immutable data structures in your components, but the React props API is semi-immutable by itself. React uses the native JavaScript Object.freeze (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) method if available to prevent new properties from being added to it or existing properties from being removed. Object.freeze also prevents existing properties (or their enumerability, configurability, or writability) from being changed and prevents the prototype from being changed, too. This goes a long way toward preventing you from mutating the props object, but it’s not technically a truly immutable object (although you can essentially think of it that way).
Props are data that gets passed to React components, either from a parent or from the defaultProps static method on the component itself. Whereas component state is localized to a single component, props are usually passed from a parent component. If you’re thinking, “Can I use state in a parent component to pass props to a child component?” you’re onto something. State in one component can be props for another.
Props are usually passed in JSX as attributes, but if you’re using React.createElement you can pass them directly into a child component via that interface. You can pass any valid JavaScript data as a prop to another component—even other components (which are only classes after all). Once props are passed into a component for use, you shouldn’t change them from within the component. You can try, but you’ll probably get a nice error like Uncaught TypeError: Cannot assign to read-only property '<myProperty>' of object '#<Object>'—or worse, your React app won’t work as expected because you’re violating the expected use.
Listing 3.4 in the next section shows some ways you can access props and how not to assign to them. As noted before, properties can change over time, but not from within the component. This is part of one-way data flow—a topic covered in later chapters. In short, one-way means changing data flows down through components from parent components to children. A parent component using state (inheriting from React.Component) can change its state, and that changed state can be what is passed down as properties to child components, thus changing properties.
Stateless functional components are functional because they can be written as named functions or anonymous function expressions assigned to a variable. They only take props and, because they return the same output based on a given input, are essentially considered pure. This makes them fast, as React will potentially be able to make optimizations by avoiding unnecessary lifecycle checks or memory allocations. The following listing shows a simple example of a stateless functional component. Run the code at https://codesandbox.io/s/l756002969.
Listing 3.5. Stateless functional components
import React from "react"; import { render } from "react-dom"; import PropTypes from "prop-types"; function Greeting(props) { #1 return <div>Hello {props.for}!</div>; } Greeting.propTypes = { #2 for: PropTypes.string.isRequired }; Greeting.defaultProps = { #2 for: 'friend' }; render(<Greeting for="Mark" />, mountNode); // Or using an arrow function // const Greeting = (props) => <div>Hello {props.for}</div>; #1 //... specify props and default props same as before // render(<Greeting name="Mark" />, document.getElementById("root")); #1 Stateless functional components can be created with functions or anonymous functions. #2 For either form of stateless functional component, you can specify propTypes and default props as properties on the function or variable. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":71},{\"line\":4,\"ch\":73}],[{\"line\":19,\"ch\":71},{\"line\":19,\"ch\":73}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":12,\"ch\":71},{\"line\":12,\"ch\":73}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":12,\"ch\":71},{\"line\":12,\"ch\":73}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}],[{\"line\":4,\"ch\":71},{\"line\":4,\"ch\":73}],[{\"line\":19,\"ch\":71},{\"line\":19,\"ch\":73}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}]]"} !@%STYLE%@!
Figure 3.4. Data flows one way in React. Props are passed from parent to child (from owner to ownee), and children can’t edit the state or props of a parent component. Each component that has a backing instance can modify its own state but can’t modify anything outside itself apart from setting the props of one of its children.
![]()
When working with props, you have a few APIs available to you that can help you during development: PropTypes and default props. PropTypes provides a typechecking functionality in which you can specify what sort of props your component will expect to receive when used. You can specify data types and even tell the component consumer what sort of shape of data they need to provide (an object with a user property that has certain keys, for example). In past versions of React, PropTypes was part of the core React library, but now lives alone as the prop-types package (https://github.com/facebook/prop-types).

This is an excerpt from Manning's book Redux in Action.
This architecture has several disadvantages. Along the way, data may filter through components that have no use for it other than to pass the data on to their children. In a large application, this can result in tons of data moving through unrelated components, passed down via props or passed up using callbacks. It’s likely that a small number of components at the top of the application end up with an awareness of most of the state used throughout the entire application. At a certain scale, maintaining and testing this code becomes untenable. Because React wasn’t intended to solve the same breadth of problems that other MVC frameworks attempted to address, an opportunity existed to bridge those gaps.
Task and TaskList will be stateless functional components, introduced in React v0.14. They don’t have access to lifecycle methods such as componentDidMount, only accept props, don’t use this.state or this.setState, and they’re defined as plain functions instead of with createReactClass or ES2015 classes.
These kinds of components are wonderfully simple; you don’t have to worry about this, they’re easier to work with and test, and they cut down on the number of lines of code you might need with classes. They accept props as input and return some UI. What more could you ask for? Copy the code in the following listing to Task.js.
Listing 2.2. src/components/Task.js
import React from 'react'; const Task = props => { #1 return ( <div className="task"> <div className="task-header"> <div>{props.task.title}</div> #2 </div> <hr /> <div className="task-body">{props.task.description}</div> </div> ); } export default Task; #1 Stateless functional components are exported anonymous functions. #2 These components receive and display props from parent components. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":46},{\"line\":2,\"ch\":48}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":2}],[{\"line\":6,\"ch\":46},{\"line\":6,\"ch\":48}],[{\"line\":17,\"ch\":0},{\"line\":17,\"ch\":2}]]"} !@%STYLE%@!
Listing 2.11 passes connect a single argument, the mapStateToProps function. Note that the name mapStateToProps is a convention, not a requirement. The name stuck for a reason: because it’s an effective descriptor of the role of this function. State refers to the data in the store, and props are what get passed to the connected component. Whatever you return from mapStateToProps will be passed to your component as props.
Listing 2.11. src/App.js: connecting components
import React, { Component } from 'react'; import { connect } from 'react-redux'; #1 import TasksPage from './components/TasksPage'; class App extends Component { render() { return ( <div className="main-content"> <TasksPage tasks={this.props.tasks} /> #2 </div> ); } } function mapStateToProps(state) { #3 return { tasks: state.tasks #4 } } export default connect(mapStateToProps)(App); #1 Adds connect to the list of imports #2 Tasks will be available via props after connected to the store. #3 The state argument is the entire contents of the Redux store, specifically the result of calling getState on the store instance. #4 The return value of mapStateToProps is passed into the App component as props, which is why render can reference this.props.tasks. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":53},{\"line\":1,\"ch\":55}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":8,\"ch\":53},{\"line\":8,\"ch\":55}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}],[{\"line\":14,\"ch\":53},{\"line\":14,\"ch\":55}],[{\"line\":24,\"ch\":0},{\"line\":24,\"ch\":2}],[{\"line\":16,\"ch\":53},{\"line\":16,\"ch\":55}],[{\"line\":25,\"ch\":0},{\"line\":25,\"ch\":2}]]"} !@%STYLE%@!Now the application successfully renders data from the Redux store! Notice how you didn’t have to update the TasksPage component? That’s by design. Because TasksPage accepts its data via props, it doesn’t care what the source of those props is. They could come from Redux, from React’s local state, or from another data library altogether.
There it is: a dispatch prop in addition to your expected tasks array. What’s dispatch? You know that the store is extremely protective of its data. It only provides one way to update state—dispatching an action. dispatch is part of the store’s API, and connect conveniently provides this function to your component as a prop. Let’s create a handler where you dispatch a CREATE_TASK action (see listing 2.14). The action will have two properties:
Container components receive data from Redux and dispatch action creators, and presentational components accept data as props and handle markup.