微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们如何在React-Redux中使用Refs?

如何解决我们如何在React-Redux中使用Refs?

我正在做一个手风琴项目,我想通过单击其中一个按钮来滚动按钮的高度。为此,我使用了Refs,它在 React 中可以正常工作,但是我必须在 React-Redux 中使用Refs,但这不起作用。

任何人都可以对此问题提出建议,请分享

这是我的减速器,通过单击以下按钮可以滚动每个按钮的高度:

import * as actionTypes from '../actions/actions';

const initialState = {
    active: false,height: '0px'
}
const reducer = (state = initialState,action) => {
    switch(action.type) {
        case actionTypes.ACTIVE_STATE:
            return {
                ...state,active: state.active === false ? true : false,height: state.active === true ? '0px' : `${this.content.current.scrollHeight}`   <---
            }
        default:
    }
    return state; 
}
export default reducer;

这是我创建参考文件的手风琴组件:

import React,{ Component,forwardRef } from 'react';
import { connect } from 'react-redux';
import * as actionTypes from '../store/actions/actions';

class Accordion extends Component {
    
    content = React.createRef();    <---
    render() {
        return (
            <div>
                <button className={`accordion ${this.props.accordions.active}`}
                onClick={this.props.expandAccordion} >
                    {this.props.title}
                </button>
                <div className="panel">
                    {this.props.content}
                </div>
            </div>
        );
    }
}
const mapStatetoProps = (state) => {
    return {
        accordions: state.data
    };
}
const mapdispatchToProps = (dispatch) => {
    return {
        expandAccordion: () => dispatch({type: actionTypes.ACTIVE_STATE})
    };
}
export default connect(mapStatetoProps,mapdispatchToProps,null,{ forwardRef: true })(Accordion);

一个建议或意见都可以帮助我。

随时分享您的想法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。