score:291
a component cannot update its own props unless they are arrays or objects (having a component update its own props even if possible is an anti-pattern), but can update its state and the props of its children.
for instance, a dashboard has a speed
field in its state, and passes it to a gauge child thats displays this speed. its render
method is just return <gauge speed={this.state.speed} />
. when the dashboard calls this.setstate({speed: this.state.speed + 1})
, the gauge is re-rendered with the new value for speed
.
just before this happens, gauge's componentwillreceiveprops
is called, so that the gauge has a chance to compare the new value to the old one.
score:0
if you use recompose
, use mapprops
to make new props derived from incoming props
example:
import { compose, mapprops } from 'recompose';
const somecomponent = ({ url, oncomplete }) => (
{url ? (
<view />
) : null}
)
export default compose(
mapprops(({ url, storeurl, history, ...props }) => ({
...props,
onclose: () => {
history.goback();
},
url: url || storeurl,
})),
)(somecomponent);
score:4
trick to update props if they are array :
import react, { component } from 'react';
import {
appregistry,
stylesheet,
text,
view,
button
} from 'react-native';
class counter extends component {
constructor(props) {
super(props);
this.state = {
count: this.props.count
}
}
increment(){
console.log("this.props.count");
console.log(this.props.count);
let count = this.state.count
count.push("new element");
this.setstate({ count: count})
}
render() {
return (
<view style={styles.container}>
<text>{ this.state.count.length }</text>
<button
onpress={this.increment.bind(this)}
title={ "increase" }
/>
</view>
);
}
}
counter.defaultprops = {
count: []
}
export default counter
const styles = stylesheet.create({
container: {
flex: 1,
justifycontent: 'center',
alignitems: 'center',
backgroundcolor: '#f5fcff',
},
welcome: {
fontsize: 20,
textalign: 'center',
margin: 10,
},
instructions: {
textalign: 'center',
color: '#333333',
marginbottom: 5,
},
});
score:5
much has changed with hooks, e.g. componentwillreceiveprops
turned into useeffect
+useref
(as shown in this other so answer), but props are still read-only, so only the caller method should update it.
score:28
props can change when a component's parent renders the component again with different properties. i think this is mostly an optimization so that no new component needs to be instantiated.
score:84
props
a react component should use props to store information that can be changed, but can only be changed by a different component.
state
a react component should use state to store information that the component itself can change.
a good example is already provided by valéry.
Source: stackoverflow.com
Related Query
- In how many ways we can pass props to all child components in react
- How can I update every React components using a context without provider?
- Even after redux state update mapStateToProps not returning updated props to components in react
- How can I pass props to React components conditionally?
- React Props wont update in Child Components
- How to update props in lower components in React from Redux store?
- Can I update a component's props in React.js?
- How can I prevent event bubbling in nested React components on click?
- Can you pass down state to child components with React Hooks?
- How to update components props in storybook
- React setState can only update a mounted or mounting component
- In React Native how can I test my components with Shallow Rendering?
- React memo components and re-render when passing function as props
- Can not update state inside setInterval in react hook
- React: Are there respectable limits to number of props on react components
- How can I dispatch from child components in React Redux?
- React router - pass props on routes to child components
- Can a React app have components in different JS files without a build system?
- HOC's and Render Props With Functional Components in React 16
- Can I edit React components without reloading the browser?
- How can I use React Material UI's transition components to animate adding an item to a list?
- How to update props of React Component rendered using ReactDOM.render()
- How I can render react components without jsx format?
- How to update (re-render) the child components in React when the parent's state change?
- React prevent remounting components passed from props
- React class components - conditional styling based on props
- How can I unit test parent react components that call methods on children
- How can React useEffect watch and update state?
- React update state in parent from child components
- How can I spread props to a React component that uses exact props when using Flow?
More Query from same tag
- Using tab key to move focus from data grid to next page element
- React: Passing child state to parent state
- How can I embed a Github Gist inside of a React component?
- Component is not rendered using useEffects
- Debugging url in javascript
- How to get mock utility function with jest
- How to take the value from selectedOption And display it
- How to use constructor value outside constructor?
- OpenWeatherMap API: cannot read property of undefined
- How to make circle as table using konva react
- Property 'value' does not exist on 'EventTarget' in TypeScript
- JavaScript: Cut out Array of Strings until a certain String matches (the elegant way)
- Extracting the ref from the forwardRef (createRef compared to useRef) | react native
- React useCallback hook: What are the correct dependencies for these handleChange & handleSubmit functions to prevent re rendering?
- Keep having the error "TypeError: Cannot read property 'map' of undefined" when using map function in React js
- React component deeper structure and props
- useState hook setter is running only once when called by an EventListener
- How to reach all input field values and submit with button in form?
- Wrong React hooks behaviour with event listener
- How to disable ALL type checking in *.test.* files (including imported components)
- Call function inside constructor of component in react?
- why custom hook , context with useEffect lead to infinite loop?
- How to make sure user enters valid Regex on an HTML form input?
- Cannot read properties of undefined (reading 'text')
- Passing Generic's to React Typescript Component
- 'useState' is not defined no-undef in REACT
- How to define the printable area of a react page?
- why does my minimax for tic-tac-toe return undefined in reactjs
- React - Change a JSON Object in setState
- Opening modal from click of a different button from parent component