React Flashcards

1
Q

What is React? What advantages react gives to us?

A

React is a JS library that allows you to structure HTML into small, DRY and manageable chunks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is JSX?

A

JSX allows us to write HTML in React.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What <div> Hello </div> actually creates inside JSX?

A

React.createElement(‘div’, null, ‘Hello’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create function-based component?

A
# First option 
const Sample = () => {
  return <div> Sample </div>
}
# Second option

const Sample = () => (

<div> Sample ! </div>

)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add react component to the existing html code?

A

ReactDOM.render(< Sample/>, document.getElementById(‘react-sample’))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create class-based component?

A
class MySample extends React.Component {
  render() {
    return <div> React Class ! </div>
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to send/receive params to class-based components?

A

< Sample question=’a?’ answer=’b’>

<div>
{ this.props.question }
{ this.props.answer }
</div>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to add css class name inside JSX?

A
# className='sample'
<div> Hello </div>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to import component from another file? And how to return default component from this file?

A
# to import to file
import Card from './components/Card'
# to return from the file 
export default Card
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is react PropTypes? Why do we need it?

A

This is a library that can check what types/values we send to components and raise warnings if something doesn’t match.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to check component params with PropTypes?

A

Card.propTypes = {
question: PropTypes.string,
answer: PropTypes.string
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to add unix id/key to compent?

A

< Card key={index} />

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to add a button and call function on click?

A
# first option 
< button onClick={deleteCard}> Delete  < /button>
function deleteCard() {
  alert('Deleted')
}

second option inside the class

class Sample {
  deleteCard() {
    alert('Deleted')
  }
 < button onClick={this.deleteCard.bind(this)}> Delete  < /button>
}
# 3rd option 
class Sample {
  deleteCard = () => {
    alert('Deleted')
  }
 < button onClick={this.deleteCard}> Delete  < /button>
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why do we need state? And how to create state?

A

We need a state to dynamically set properties because this.props we can use only to read data.

class App
  state = {
    visible: true 
  }

# access
this.state.visible
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to set state?

A

this.setState({ visible: false })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to run parent function from the child component?

A
# parrent
< App hide_card={this.hideCard} />
# child
this.props.hideCard()
17
Q

How to add form and handle it on submit?

A

createCard = (event) => {
event.preventDefault(); # we use it to prevent redirect
}

< form onSubmit={this.props.createCard}>
< /form>

18
Q

Why do we need ref? And how to create ref?

A

Ref allows us to bind input fields with variables and get data from them without using document.getElementById

class Samle  {
  constructor(props) {
    super(props)
    this.question_input = React.createRef()
  }
  sample() {
   # return the value
    this.question_input.current.value
  }

< input ref={this.question_input} />
}