简体   繁体   English

React-从另一个组件设置组件的属性

[英]React - Set property of a component from another Component

I am complete noob to react so please bear with me. 我是完全菜鸟,所以请耐心等待。

I am creating a webpage that has dusting on react components on it. 我正在创建一个网页,上面有灰尘。 I am not creating the whole page using react and this isn't an option. 我没有使用react创建整个页面,这不是一个选择。

What i want I want to do is load a json file and use the data to set the properties of the components on the page. 我想要做的是加载一个json文件并使用该数据设置页面上组件的属性。 These properties won't be changing once the page has rendered. 页面呈现后,这些属性将不会更改。

I have managed to load the json and set the properties of a component. 我设法加载json并设置组件的属性。 I also need to reuse some of the properties of this component in other components that are not a children of the first component. 我还需要在不是第一个组件的子组件的其他组件中重用该组件的某些属性。

I suppose very basically I need to create a global set of data that can be used by any component on the page. 我想基本上我需要创建一组全局数据,供页面上的任何组件使用。 I'm coming from an angular background so maybe this is throwing me off course a bit 我来自一个有角度的背景,所以也许这会使我偏离路线

Here is what I have so far 这是我到目前为止的

    var ReleaseList=React.createClass({

      getInitialState: function(){
        return {
          allReleases:[]
        }
      },
      showResults :function(ReleaseData){

        this.setState({
          allReleases :ReleaseData
        });
      },

      getReleases:function(){
        fetch('http://imac.local:5757/s0dj8a.json').then(function(response) {
            // Convert to JSON
            return response.json();
            }).then(function(j) {
                var ReleaseData=j.releaseDetail[0]
                this.showResults(ReleaseData)
            }.bind(this));
      },
      componentDidMount(){
        this.getReleases();
      },

      render:function(){
    return (

            <h1>{this.state.allReleases.title}</h1>
      )

      }
    });

    var ReleaseDataTitle=React.createClass({
      render:function(){
    return (
      <div>
              <h2>{this.props.title}</h2>
              // This needs to be the same as this.state.allReleases.title in the previous component

      </div>
      )
      }

      });

    ReactDOM.render(<ReleaseList/>, document.getElementById('content'));
    ReactDOM.render(<ReleaseDataTitle title={'I need this to be the same as this.state.allReleases.title in <ReleaseList />'}/>, document.getElementById('banner'));

I wouldn't consider React to render small static parts of your website just as I wouldn't use jQuery if I know JS well - it's unnecessary download for such a simple task. 我不会考虑使用React来呈现您网站的静态小部分,就像我不太了解JS一样,我不会使用jQuery一样-对于如此简单的任务,这是不必要的下载。 I would however consider Preact , a 3kb React clone that was made for these kind of situations: small parts of site / app with similar API. 但是,我将考虑Preact ,它是为以下情况而制作的3kb React克隆:站点/应用程序的一小部分具有相似的API。

But here's how I would do it with React (I imagine Preact is very similar). 但是这是我将如何使用React的方法(我想Preact非常相似)。


These properties won't be changing once the page has rendered. 页面呈现后,这些属性将不会更改。

If the data doesn't change, you don't even need to store the data to global variable, just render React components while passing the fetched data as props . 如果数据没有改变,您甚至不需要将数据存储到全局变量,只需渲染React组件,同时将获取的数据作为props传递即可。

fetch('http://imac.local:5757/s0dj8a.json').then(function(response) {
    return response.json()
}).then(function(data) {

     ReactDOM.render(<First data={data} />, document.getElementById('first'))
     ReactDOM.render(<Second data={data} />, document.getElementById('second'))

     // And any other React component you wish to add to DOM

})

Now the data is available in each of these components as this.props.data . 现在,这些元素中的每个数据都可以使用this.props.data Now you can do whatever you want with this data: format, change, pass to children, render etc . 现在,您可以使用此数据做任何您想做的事情:格式化,更改,传递给子代,渲染

PS! PS! Me and even React creators recommend to use ES6 classes, especially if you just started .. 我甚至是React的创建者都建议使用ES6类,尤其是如果您刚刚开始..

The key is something you've already mentioned in your question. 关键是您在问题中已经提到过。 You need a global state where you can store the data required by your components. 您需要一个全局状态 ,可以在其中存储组件所需的数据。 You will also need some methods to do the data distribution. 您还将需要一些方法来进行数据分发。

For the sake of easy maintenance, predictability and reliability of our apps, I believe React components should be agnostic about data (which means they should only represent the View layer, and shouldn't handle API calls or data processing by themselves). 为了易于维护,我们应用程序的可预测性和可靠性,我相信React组件应与数据无关 (这意味着它们仅应代表View层,而不应自行处理API调用或数据处理)。

Since your website is not completely built with react, you can delegate vanilla javascript methods for data fetch and pass it as a props to your React components. 由于您的网站不是完全使用react构建的,因此您可以委托香草javascript方法进行数据提取,并将其作为道具传递给React组件。 If you ever plan to fully migrate to React, you can make use of Redux , MobX , etc. to maintain your global state. 如果您打算完全迁移到React,则可以使用ReduxMobX等来维护您的全局状态。

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

相关问题 React Native,将道具从一个组件导航到另一个组件 - React Native, Navigating a prop from one component to another 如何将 json 文件中的元素设置为 React 组件 - How to set elements from a json file into a React component 来自React组件的REST调用 - REST calls from a react component 将JSON数据从App组件传递到Angular 6中的另一个组件 - Pass JSON data from App Component to another component in Angular 6 如何将状态从有状态组件传递到另一个有状态组件 - How to pass state from a stateful component to another stateful component 将 ID 从一个组件导出到 angular 中的另一个组件 - Exporting ID from one component to another component in angular 从 Functional React Component 中的 JSON Response 中获取一个特定值并将其设置为 Select 选项 - Get one specific value from JSON Response in Functional React Component and set it as a Select option 如何通过返回的json对象中的特定项设置react组件的状态? - How to set state of a react component with a specific item from a returned json object? 反应如何将反应数组选择的数组数据一个组件传递给另一个组件 - React how to pass react array selected array data one component to another component React - 在组件状态的数组内向对象添加属性 - React - Adding a property to an object inside of array in component's state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM