You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
handleSubmit(event){alert('A name was submitted: '+this.input.value);event.preventDefault();}<inputdefaultValue="Bob"type="text"ref={(input)=>this.input=input}/>
Thinking in React
Step 1: Break The UI Into A Component Hierarchy
Single Responsibility Principle: A component should ideally only do one thing.
Step 2: Build A Static Version in React
Don't use state at all to build this static version.
You can build top-down or bottom-up. In simpler examples, it's usually easier to go top-down, and on larger projects, it's easier to go bottom-up and write tests as you build.
Step 3: Identify The Minimal (but complete) Representation Of UI State
DRY: Don't Repeat Yourself.
Is it passed in from a parent via props? If so, it probably isn't state.
Does it remain unchanged over time? If so, it probably isn't state.
Can you compute it based on any other state or props in your component? If so, it isn't state.
Step 4: Identify Where Your State Should Live
Identify every component that renders something based on that state.
Find a common owner component (a single component above all the components that need the state in the hierarchy).
Either the common owner or another component higher up in the hierarchy should own the state.
If you can't find a component where it makes sense to own the state, create a new component simply for holding the state and add it somewhere in the hierarchy above the common owner component.
React
参考资源
基本概念
<input type="text" value={this.state.value} onChange={this.handleChange} />
核心 API
ReactDOM
最佳实践 Best Practices
JSX
参考:
Components and Props
Extracting Components
Props are Read-Only
All React components must act like pure functions with respect to their props.
State
setState(nextState, callback)
setState(nextState, callback)
Using State Correctly
Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.
Event
Forms
Controlled Components
Uncontrolled Components
Thinking in React
Step 1: Break The UI Into A Component Hierarchy
Single Responsibility Principle: A component should ideally only do one thing.
Step 2: Build A Static Version in React
Don't use state at all to build this static version.
You can build top-down or bottom-up. In simpler examples, it's usually easier to go top-down, and on larger projects, it's easier to go bottom-up and write tests as you build.
Step 3: Identify The Minimal (but complete) Representation Of UI State
DRY: Don't Repeat Yourself.
Step 4: Identify Where Your State Should Live
Step 5: Add Inverse Data Flow
SOLID
面向对象设计五个基本原则 SOLID:
参考:
Yarn vs npm
Higher-Order Components
总结
The text was updated successfully, but these errors were encountered: