본문 바로가기
창고 3/[Dev] My Readme

Learning React Essentials 4

by 부엉이의 정보 창고 2022. 1. 24.
728x90

Unidirectional data flow in React

React has a unidirectional data flow, meaning that data only flows from parent component to child one. The chilld component only receives the state data they need. State management is done in one part of codes and UI rendering is done in another part(separation)

class MyApp extends React.Component { 
    constructor(props) { 
        super(props)
        this.state = { 
            name: "Jake"
        }
    }
    // class component must have a render method
    render() { 
        return (
            <Person name={this.state.name} />
        )
    }
}

class Person extends React.Component {
    constructor(props) { 
        super(props)
    }
    render() { 
        return (
            <h1>My name is : {this.props.name}</h1> // passing state as props
        )
    }
}
 

You can also pass function, method. This is React way of communicating between parent component and child component. The passed state/function can be accessed like below.

this.props.(its name)
// delivering 'input', 'handleChange' attributes to custom HTML tag GetInput */}
<GetInput input={this.state.inputValue} handleChange={this.handleChange}/>

Styling in React

Inline styles is very common in React development. Set property and value in Javascript object form with camel case naming. Any hyphenated style properties should be converted to camel case in JSX.

class Colorful extends React.Component {
    render() {
      return (
        <div style={
            {
                color: "red",
                fontSize: 72 // unit px is omitted
            }
        }>Big Red</div>
      );
    }
  };

For a larget set of styles, create a style object in a global constant variable to manage that.

const styles = {
    color : "red",
    fontSize : 40, 
    border : "2px solid yellow" // do not add comma between.
}
728x90

'창고 3 > [Dev] My Readme' 카테고리의 다른 글

Learning HTML5 Essentials  (0) 2022.01.31
Learning Typescript Essentials  (0) 2022.01.28
Learning React Essentials 3  (0) 2022.01.21
Learning React essentials 1  (0) 2022.01.18
Understanding asynchronous Javascript  (0) 2022.01.17

댓글


loading