Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Virtual DOM, Events, and Forms #12

Open
zxw018018 opened this issue Sep 21, 2018 · 0 comments
Open

React Virtual DOM, Events, and Forms #12

zxw018018 opened this issue Sep 21, 2018 · 0 comments

Comments

@zxw018018
Copy link
Owner

zxw018018 commented Sep 21, 2018

Virtual DOM

  • A data structure stored by React that tracks changes from one render state to the next
  • If something has changed from one render to the next, the browser's DOM is update (Reconciliation)

Synthetic Events

Supports all the native browser events, but provides a consistent API on all browsers

Events

bond function

class ClickExample extends Component {
    constructor(props) {
        super(props);
        this.state = {name: "tim"};
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick(e) {
        this.setState((prevState, props) => ({
            name: prevState.name.toUpperCase()
        });
    }
    render() {
        return (
            <div>
                <p>{this.state.name}</p>
                <button type="button" onClick={this.handleClick}>
                    UPPERCASE
                </button>
            </div>
        );
    }
}

inline calbacks

render() {
    return (
        <div>
            <p>{this.state.name}</p>
            <button type="button"
                onClick={() => this.setState({name: "TIM"})}>
                UPPERCASE
            </button>
        </div>
    );
}

Form

<form onSubmit={(e) => {
    e.preventDefault();
    const data = [...this.state.data, this.state.inputText];
    this.setState({data, inputText: ''});
}}>
    <input
        type="text"
        name="inputText"
        value={this.state.inputText}
        onChange={(e) => {
            this.setState({[e.target.name]: e.target.value})        
        }}
    />
</form>

refs

  • A direct reference to a DOM element

Use Cases

  • Managing focus, text selection, or media playback
  • Triggering imperative animations
  • Integrating with third-party DOM libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant