Hello,
Maybe I forgotten something, but I wonder how to add directly one property to a Fonctional Component?
In the courses it seems to me that I saw how to put a property passinf by another component but not direcly
For exemple, I would like add one props property “color: red” to this Fonctional Component:
const Fruits = () => {
return (
);
};
How you do that ?
Furthermore, I wonder how to display all properties of props in this case ?
props you pass to react components are found in the props objects, which is the first parameter react components get by default. For example you can access it this way:
The props object purpose is to take properties from the outside and be used within the component. I dont see what the purpose would be to add additional props to the propcs obj from within.
My example was not good. it’s more like a tantative to modify a property.
Your answer is more in line with what I wanted to do.
Now if you tell me that it makes no sense to fill in the properties inside the Component, I will wait to go further in the course to realize it by myself.
React is probably calling Object.freeze() on props before it is passed down to the child component. Object.freeze() ‘freezes’ an object so that no properties can be added or changed.
First of all, don’t do it. Don’t mess with props and don’t mutate state directly - that will cause React to not work correctly. Secondly, in JS you can freeze objects so they can’t change them.
But why do you need to? What are you trying to accomplish?
OK, but “props”, by definition, are values that are injected into the component from outside. If you want to change the props, change them outside the component, when they’re passed in.