【问题标题】:how do I dynamically render title as component如何将标题动态呈现为组件
【发布时间】:2018-01-22 00:55:35
【问题描述】:

我有一个场景:

<Scene key="myFeed" component={myFeed} renderTitle={level}/>

我在其中使用组件来显示“标题”。 这个函数:

const level = () => {
  return (
     <Level/>
  )
}

这个组件:

export class Level extends Component {
    constructor(props) {
        super(props);


        this.state = {
            progressBar: {},
            loading: true,
        };

    }

    connection() {
        myApi.getServer('/user/level/', (data) => {
            this.setState({loading: false, progressBar: data});
        }, (err) => {
            console.log(err.message);
            this.setState({loading: false});
        })
    }

    render() {
        return(
            <View  style={styles.topBarCenterSlider}>
                   <View style={styles.topBarSlider}>
                          <View style={[styles.topBarStatus, {width: this.state.progressBar.level["progress"] + '%' : 0}]}/>
                          </View>
                   </View>
            </View>
        );
    }
}

我需要在服务器响应后更改组件。 请帮忙,对不起我的英语))

【问题讨论】:

    标签: reactjs react-native render react-native-router-flux


    【解决方案1】:

    我看到你没有调用“connection()”。放入组件WiLlMount

    componentWillMount() {
      this.connection();
    }
    

    【讨论】:

    • 查看width: this.state.progressBar.level["progress"] + '%' : 0。尝试更改为width: this.state.progressBar.level ? this.state.progressBar.level["progress"] + '%' : 0
    • 还有一个问题可以吗?为什么“连接”功能被触发两次? componentWillMount() { (this.state.loading) ? this.connection() : null; }
    • 我不知道为什么函数“连接”被触发了两次。但是你不需要 put (this.state.loading) 吗? this.connection() : null。原因 componentWillMount 只会触发一个。查看更多文档facebook.github.io/react/docs/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多