Component Props and State
This sample gives an basic example of using props and state in your components.
Source Code
https://github.com/charleslbryant/hello-react-and-typescript/releases/tag/0.0.4
src/main.tsx
The change to main.tsx is minor. Here is the one change.
We are just passing defaultName
to the HelloWorld
component. This is doing exactly what you think it is, its setting the default value for who we are saying Hello to. Notice that this name is explicit in defining this input. It is a default value, the HelloWorld component can change this value and the assumption is that it may be changed.
Another thing to notice is the camelCasing.
All DOM properties and attributes (including event handlers) should be camelCased to be consistent with standard JavaScript style. https://facebook.github.io/react/docs/dom-differences.html
src/helloworld.tsx
We made two changes to helloworld.tsx, but they are significant to the interactivity of our little application.
In the constructor we are setting the initial state for this component. this.state
is a plain JavaScript object. It is a mutable representation of the model for the view. this.state
is expected to change over time as events occur in the component. In this sample we are initializing this.state
with an object literal { name: this.props.defaultName }
.
this.props.defaultName
was passed in from main.tsx. this.props
is also a plain JavaScript object. The difference being it is an immutable representation data used by the component. this.props
should not be changed and they are passed in by parent components. If you change props, they will be overwritten when the parent rerenders. Mutating props puts your component in an inconsisten state, so don't do it.
The next change is to the return value of the render
method. We are using this.state.name
to set the name dispayed in the UI.
In this sample we use this.props
to initialize this.state
. We know that we want to update the name of who we say hello to, so we are using a state object to represent the name. If we didn't want to update name, we could use props directly. For example
Using props like this is saying that we don't expect this component to change the name. In fact, to make it explicit, we changed the name from defaultName
to name
. Also, main.txt
would have to be changed from passing defaultName
to name
. Using props means we expect the parent to be in control of changing the value of props. Using state means we expect the component to be in control of its own state.
Last updated