react-native TextInput如何根据传入的值初始化高度

constructor() {
        super();

        this.state = {
            content: '',
            height: 0,
            title:''
        };
    }
        componentDidMount() {
        this.setState({
            content: this.props.content
        })
    }
changeContent = (event)=> {
        console.log(event.nativeEvent);
        this.setState({
            content: event.nativeEvent.text,
            height: event.nativeEvent.contentSize.height
        });
    }
    .....
<TextInput
                        autoFocus={true}
                        multiline={true}
                        onChange={this.changeContent}
                        underlineColorAndroid="transparent"
                        style={[styles.textInputStyle, {height: Math.max(150, this.state.height)}]}
                        value={this.state.content}
                    />
                .....

现在当在输入框输入东西的时候,输入框的高度会变高,但是初始化的时候高度只有150。如果componentDidMount的的时候传入的props.content内容很多,该如何在刚开始的时候就获取到正确的高度?
请问一下各位有什么好的想法?

阅读 2.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题