React Flashcards
What is React? What advantages react gives to us?
React is a JS library that allows you to structure HTML into small, DRY and manageable chunks.
What is JSX?
JSX allows us to write HTML in React.
What <div> Hello </div> actually creates inside JSX?
React.createElement(‘div’, null, ‘Hello’)
How to create function-based component?
# First option const Sample = () => { return <div> Sample </div> } # Second option
const Sample = () => (
<div> Sample ! </div>
)
How to add react component to the existing html code?
ReactDOM.render(< Sample/>, document.getElementById(‘react-sample’))
How to create class-based component?
class MySample extends React.Component { render() { return <div> React Class ! </div> } }
How to send/receive params to class-based components?
< Sample question=’a?’ answer=’b’>
<div>
{ this.props.question }
{ this.props.answer }
</div>
How to add css class name inside JSX?
# className='sample' <div> Hello </div>
How to import component from another file? And how to return default component from this file?
# to import to file import Card from './components/Card' # to return from the file export default Card
What is react PropTypes? Why do we need it?
This is a library that can check what types/values we send to components and raise warnings if something doesn’t match.
How to check component params with PropTypes?
Card.propTypes = {
question: PropTypes.string,
answer: PropTypes.string
}
How to add unix id/key to compent?
< Card key={index} />
How to add a button and call function on click?
# 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> }
Why do we need state? And how to create state?
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 to set state?
this.setState({ visible: false })