redux更新了却没有重新渲染?

//reducer

const initState={

    style:{left:0}

}

const Tabs=(state=initState,action)=>{

    switch (action.type){

        case 'recommend':

            Object.assign(state,{style:{left:0}})

            return state

        case 'list':

            Object.assign(state,{style:{left:'25%'}})

            return state

        default:

            return state

    }

}

export default Tabs

//组件

 export default class Tab extends Component{

    render(){

        const {style}=this.props

        return(

            <div>

                      ...

                    <div style={style}><span></span></div>

            </div>

        )

    }

}


一只甜甜圈
浏览 1071回答 1
1回答

宝慕林4294392

const Tabs=(state=initState,action)=>{&nbsp; &nbsp; switch (action.type){&nbsp; &nbsp; &nbsp; &nbsp; case 'recommend':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Object.assign({style:{left:0}},state);&nbsp; &nbsp; &nbsp; &nbsp; case 'list':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Object.assign({style:{left:'25%'}},state);&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return state&nbsp; &nbsp; }}redux是覆盖式更新的不能把旧的state对象返回回去,必须是新建的对象才会更新
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript