/
githubmirror
/
redux
Обзор
Документация
Войти
/
githubmirror
/
redux
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/tree-view/src/containers/Node.js
73 строки
2 KB
Tim Dorr
Format the examples
20 мар 2024, 19:52
Не верифицирован
20 мар 2024, 19:52
3085048
Код
Авторство
О чём код?
import React from 'react' import { Component } from 'react' import { connect } from 'react-redux' import * as actions from '../actions' export class Node extends Component { handleIncrementClick = () => { const { increment, id } = this.props increment(id) } handleAddChildClick = e => { e.preventDefault() const { addChild, createNode, id } = this.props const childId = createNode().nodeId addChild(id, childId) } handleRemoveClick = e => { e.preventDefault() const { removeChild, deleteNode, parentId, id } = this.props removeChild(parentId, id) deleteNode(id) } renderChild = childId => { const { id } = this.props return ( <li key={childId}> <ConnectedNode id={childId} parentId={id} /> </li> ) } render() { const { counter, parentId, childIds } = this.props return ( <div> Counter: {counter}{' '} <button onClick={this.handleIncrementClick}>+</button>{' '} {typeof parentId !== 'undefined' && ( <a href="#" onClick={this.handleRemoveClick} // eslint-disable jsx-a11y/anchor-is-valid style={{ color: 'lightgray', textDecoration: 'none' }} > × </a> )} <ul> {childIds.map(this.renderChild)} <li key="add"> <a href="#" // eslint-disable jsx-a11y/anchor-is-valid onClick={this.handleAddChildClick} > Add child </a> </li> </ul> </div> ) } } function mapStateToProps(state, ownProps) { return state[ownProps.id] } const ConnectedNode = connect(mapStateToProps, actions)(Node) export default ConnectedNode