首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react中将状态从JSX传递到JSX

在react中将状态从JSX传递到JSX
EN

Stack Overflow用户
提问于 2020-11-19 06:41:31
回答 2查看 68关注 0票数 0

所以我不知道如何使用React将我的navBar的状态传递给我的地图。我从导航栏中获取日期,只有当日期匹配时,我才需要在地图中使用它来显示内容。

代码语言:javascript
复制
class NavSearchBar extends Component {

    constructor(){
        super();
        this.state = {
            startDate: moment().startOf('month'),
            endDate: moment().startOf('month'), 
    }//these get updated somewhere else in the code
    ...

代码语言:javascript
复制
class MyMap extends Component {
   OnEachCountry = (country, layer) => { // loops thru every country
        const country_name = country.properties.NAME; // name of country
        country.properties.reported_incidents = 0; // reported incidents
        for(var tweet2 = 0; tweet2<tweet_loc.length;tweet2++){ // loops thru tweet list
            var Location_Name = JSON.stringify(tweet_loc[tweet2].user.location);
            if(Location_Name.includes(country.properties.NAME) || 
               Location_Name.includes(country.properties.ADM0_A3)){ 
                country.properties.reported_incidents++;
              // in this for loop I want to check if its between the dates too so like 
              //nav.state.startDate
            }
        }
}

从这个项目开始,我对React、Javascript、css、just文件都很陌生。所以在循环的地方,我想获取类似Navbar.state.startDate的东西,这样我就可以将它与我的地图文件中的日期进行比较。

EN

回答 2

Stack Overflow用户

发布于 2020-11-19 07:21:12

你可以通过道具传递状态。

代码语言:javascript
复制
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}

这里有官方的文档。https://reactjs.org/docs/components-and-props.html#gatsby-focus-wrapper

票数 1
EN

Stack Overflow用户

发布于 2020-11-19 06:50:16

只需在一个组件中创建整个网站,就不需要传递任何东西了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64902566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档