尝试在react-redux

时间:2019-07-19 17:07:38

标签: react-redux crud

我正在尝试在Redux中更新数据,但是当我的状态中有多个值时出现错误。

我如何将数据传输到下面的AllPalletes组件中:

<Route exact path='/' render ={(routeProps) => <AllPalletes data = {this.props.palleteNames} />} />

AllPalletes组件,我将在其中设置编辑表单:

class connectingPalletes extends Component {  

    render () {
        console.log(this.props)
        return (
            <div>
            <Menu inverted>
            <Menu.Item header>Home</Menu.Item>
            <Menu.Item as = {Link} to = '/createpalette'>Create a Palette</Menu.Item>
            </Menu>
            <Container>
            <Card.Group itemsPerRow={4}>
            {this.props.data.map((card) => {
                let cardName = card.Name.replace(/\s+/g, '-').toLowerCase()
                return (
                    <Card key = {card._id}>
                        <Image src = 'https://images.pexels.com/photos/1212406/pexels-photo-1212406.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' wrapped ui={false}/>
                        <Card.Content>
                        <Grid>
                        <Grid.Column floated = 'left' width ={7}>
                        {card.edit? (
                            <PaletteEditForm {...card}/>
                        ) : (
                            <Card.Header as = {Link} to = {`/palette/${cardName}`}>{card.Name}</Card.Header>
                        )}

                        </Grid.Column>
                        <Grid.Column floated = 'right' width = {5}>
                        <Icon name = 'pencil' />
                        <Icon name = 'trash' onClick = {() => this.props.dispatch(removePalette({id: card._id}))}/>
                        </Grid.Column>
                        </Grid>
                        </Card.Content>
                        </Card>
                )
            })}
            </Card.Group>
            <Divider></Divider>
            <Divider hidden></Divider>
            <Grid centered columns={1}>
            <Button as = {Link} to = '/testing'>Go Back</Button>
            </Grid>
            </Container>
            </div>
        )
    }
}
const AllPalletes = connect()(connectingPalletes)

export default AllPalletes

这是编辑表单:

class EditForm extends Component {
    constructor(props) {
        super(props)
        this.state = {
            paletteName: this.props.Name
        }
    }

    handleChange = (e) => {
        const val = e.target.value,
              s_name = e.target.name
        this.setState (() => {
            return {
                [s_name]: val, 
            }
        })
    }

    handleSubmit = () => {
        let updates = {Name: this.state.paletteName, edit: false}
        this.props.dispatch(editPalette(this.props._id, updates))

    }

    render() {
        console.log(this.props)
        return (
            <Form onSubmit = {this.handleSubmit}>
            <Input type = 'text' name = 'paletteName' value = {this.state.paletteName} onChange={this.handleChange} />
            </Form>

        )
    }
}

const PaletteEditForm = connect()(EditForm)

export default PaletteEditForm

我的减速机:

import uuid from 'uuid/v1'
const paletteDefault = [{
    Name: "Material UI",
    myArray: [],
    _id: uuid(),
    edit: false
}, {
    Name: "Splash UI",
    myArray: [],
    _id: uuid(),
    edit: true
}]

const PaletteReducers = (state = paletteDefault, action) => {
    console.log(action)
    switch(action.type) {
        case 'ADD_PALETTE':
            return [...state, action.palette]
            case 'REMOVE_PALETTE':
                return state.filter(x => x._id !== action.id)
                case 'EDIT_PALETTE':
                    return state.map((palette) => {
                        if(palette._id === action.id) {
                            return {
                                ...palette,
                                ...action.updates
                            }
                        }
                    })
        default:
            return state
    }
}

export default PaletteReducers

My Action

// EDIT_PALETTE

const editPalette = (id, updates) => ({
    type: 'EDIT_PALETTE',
    id,
    updates
})

export {addPalette, removePalette, editPalette}

我觉得问题可能出在我如何设置减速器箱上。

仅当我在状态中具有一个值时,编辑调度才起作用。否则,我会收到此错误:

Uncaught TypeError: Cannot read property 'Name' of undefined
at AllPalletes.js:23

请帮助。

2 个答案:

答案 0 :(得分:0)

您需要将mapStateToProps函数传递给connect(mapStateToProps)(EditForm)才能从化简器中获取名称。

const mapStateToProps = state => ({
   Name: state.PaletteReducers.Name, 
)};

const PaletteEditForm = connect(mapStateToProps)(EditForm)

答案 1 :(得分:0)

我发现了错误。在if语句之后,在“ EDIT_PALETTE”情况下,我没有给出返回值。是

case 'EDIT_PALETTE':        
return state.map((palette) => {
 if(palette._id === action.id) {
 return {
...palette,
...action.updates
    }
  }

})

应该是:

 case 'EDIT_PALETTE':
return state.map((palette) => {
 if(palette._id === action.id) {
 return {
  ...palette,
  ...action.updates
      }  
     }
  return palette
})