【问题标题】:Access Other component variable/states访问其他组件变量/状态
【发布时间】:2015-08-20 09:59:47
【问题描述】:

我想访问 TotalTab/ProfitTab/LossesTab 中的状态数据或投资组合。我希望他们在异步 fetchData 也完成时更新数据(获取数据绑定?)。

我的代码如下

class Portfolio extends Component{

    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource : ""
        };
    }

    componentDidMount() { //This function get called once when react finished loading. Something like jQueryDom Ready seems to be https://facebook.github.io/react-native/docs/tutorial.html
       this.fetchData();
    }

    fetchData() {
        fetch("http://beta.setmonitor.com/api/trading/summary?portfolio_id=3")
        .then((response) => response.json())
        .then((responseData) => {
            this.setState({
               dataSource: responseData,
               isLoading: false
            });
        })
        .done();
    }

    render() {
        return (
          <ScrollableTabView>
            <TotalTab tabLabel="Total" />
            <ProfitTab tabLabel="Profit" />
            <LossesTab tabLabel="Losses" />
          </ScrollableTabView>
        );
    }
};

我尝试创建另一个类来获取数据并存储并像new ClassName 一样使用它,但问题是异步数据在视图中没有得到更新。

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    利用道具。你可以将数据向下传递给Tab 组件,React 会自动保持同步。如果需要,您还可以在 Portfolio 组件中声明一个函数,并根据需要将其传递给子级。 React Docs - Data Flow

    【讨论】:

    • 非常感谢,这正是我需要的 :)
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多