简体   繁体   English

React:内联样式未应用于子组件

[英]React: Inline styles not being applied in child component

Objective : I have a Main component and a Textarea component.目标:我有一个 Main 组件和一个 Textarea 组件。 I would like to dynamically size the textarea and its parent container given the scrollHeight of the textarea.我想根据 textarea 的 scrollHeight 动态调整 textarea 及其父容器的大小。 Additionally, I also have a Button component, which is a child of Main, that will clear the text from the textarea, but that isn't my immediate concern.此外,我还有一个 Button 组件,它是 Main 的子组件,它将清除 textarea 中的文本,但这不是我的直接关注点。 Just wanted to add some more context.只是想添加更多上下文。

Issue : At present, the Main component reflects the correct initial and changed state values.问题:目前,Main 组件反映了正确的初始值和更改后的状态值。 However, the breakdown happens when I try to pass the updated parentHeight and textareaHeight values down to the child component, convert to string, and set the CSS properties within the style property.但是,当我尝试将更新的 parentHeight 和 textareaHeight 值向下传递给子组件、转换为字符串并在 style 属性中设置 CSS 属性时,就会发生故障。

As an aside, I've included commented code blocks to illustrate some of the other directions I've gone.顺便说一句,我已经包含了注释代码块来说明我已经走了的其他一些方向。

import Textarea from '../textareas/Textarea';
import Buttons from '../button-group/Buttons';

export default class Main extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            textareaIsEmpty: true,
            textareaHeight: 'auto',
            parentHeight: 'auto',
        }

        this.handleChange = this.handleChange.bind(this);
    }

    handleChange = (textareaHeight, parentHeight) => {

        this.setState({
            parentHeight: parentHeight,
            textareaHeight: textareaHeight,
        });
    }

    handleClick = () => {


        this.setState({
            textIsCleared: false,
        });
        
        console.log(this.textIsCleared);
    }

    render() {
        return (
            <section className="section">
                <Textarea handleChange={this.handleChange} />
                <Buttons onClick={() => this.handleClick()} />
                <Textarea handleChange={this.handleChange} />
            </section>
          );
    }
}

export default class Textarea extends React.Component {
    constructor(props) {
        super(props);

        // this.state = {
        //     parentStyle: {
        //         minHeight: this.props.parentHeight,
        //     },
        //     textareaStyle: {
        //         height: this.props.textareaHeight,
        //     },
        // }

        this.parentStyle = {
            minHeight: `${this.props.parentHeight}px`,
        }
        
        this.textareaStyle = {
            height: `${this.props.textareaHeight}px`,
        }

       this.onChange = this.onChange.bind(this);

    }

    onChange = (e) => {

        const newTextareaHeight = e.target.scrollHeight;
        const newParentHeight = e.target.parentElement.scrollHeight;
        
        
        this.props.handleChange(newTextareaHeight, newParentHeight);

        this.setStyles();
    }

    setStyles = () => {

        // this.setState({
        //     parentStyle: {
        //         ...this.state.parentStyle,
        //         minHeight: `${this.props.parentHeight}px`,
        //     },
        //     textareaStyle: {
        //         ...this.state.textareaStyle,
        //         height: `${this.props.textareaHeight}px`,
        //     },
        // })

        
    }

    render() {

        const parentStyle = `${this.props.parentHeight}px`;
        const textareaStyle = `${this.props.textareaHeight}px`;

        return (
            <div className="content_wrap" style={{minHeight: parentStyle}}>
            <textarea
                
                className="content_textarea"
                onChange={this.onChange} 
                rows={1}
                style={{height: textareaStyle}}
    
            />                      
        </div>
        )
    }
}

问题可能在于,在Textarea组件中,您试图从 props 读取parentHeighttextareaHeight值,但在Main组件中,您没有将这些 props 传递给子Textarea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM