GithubHelp home page GithubHelp logo

Comments (10)

prestifidibs avatar prestifidibs commented on August 23, 2024 11

Also, "Can change inside Component? No" and "Can change in child Components? Yes" seem paradoxical. Am I missing something?

from react-guide.

NiGhTTraX avatar NiGhTTraX commented on August 23, 2024 10

@meligatt the this.props object can't change from within the component that uses it. The props of a component are the source of truth coming from another component. The child component shouldn't ever change that truth.

Since the props of one component can be another component's state, that can change freely.

Think of components in terms of functions:

var X = {y: 21};

function foobar(props) {
  props.y = 42;
}

Calling foobar(X) would mutate the object X which is really bad for the other services that use X.

You can call foobar with different objects or different values for the keys at any point in time, but you should have the guarantee that what you're passing in will never change inside foobar. Hence, your components should use callbacks to signal the wish of updating the props. The parent components should listen to these callbacks and take the necessary actions.

from react-guide.

ovidiuch avatar ovidiuch commented on August 23, 2024

You can, technically, but it's a bad practice. It will even throw a React warning.

These are guidelines we created to have a more maintainable code, not technical specs.

from react-guide.

ovidiuch avatar ovidiuch commented on August 23, 2024

I think the correct phrasing would've been "Can change for child Components?"

from react-guide.

meligatt avatar meligatt commented on August 23, 2024

I have a question, in every react js tutorial/documentation it says that Props can't change, which I kinda understand, so, this means that the Value of a prop is the thing that can't change, right?, or it refers to the idea of adding more {key,value} to the 'props js plain object? maybe that is the thing that cannot change? i'm a little bit confused sometimes... I hope I could explain myself. thanks, cheers.

from react-guide.

meligatt avatar meligatt commented on August 23, 2024

someone was confused about the table too: here the question and answer in stackoveroflow: http://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react

from react-guide.

maxkuzmin avatar maxkuzmin commented on August 23, 2024

Hello, I have a question about "Can get initial value from parent Component?" and "Can set initial value for child Components?". Props can be set by parent component, yes, but why state is marked as "Yes"? How can parent component set state of child component?

getInitialState: function() {
   return {foo: this.props.foo}
}

This code sets initial state based on props passed by parent component, but I would not say that in this code "initial state value is set by parent component."

from react-guide.

nighthunter3173 avatar nighthunter3173 commented on August 23, 2024

Hello ! The react documentation explains that props are read only. On the other hand after experimentation, the method that I use and that seems to me adapted is to implement a method in the component parent, this method will be responsible for modifying its state, then to pass this method to the component child. By calling this method in the child component, the state of the parent component will be changed. So the only source of truth remains the parent component. I hope this can help some!

from react-guide.

papaponmx avatar papaponmx commented on August 23, 2024

I ran into some similar issue. You might want to look into React.cloneElement(). Here is the link to docs for reference: https://reactjs.org/docs/react-api.html#cloneelement

from react-guide.

emayom avatar emayom commented on August 23, 2024

While this issue is quite old 😅, I wanted to provide a reference as some individuals might still have misconceptions. According to the React post(October 17, 2015), the setProps has been deprecated.

React v0.14 - "setProps and replaceProps are now deprecated."

from react-guide.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.