react 能不能跨文件获取组件的属性?

定义了一个skill组件,点击它会打开一个新页面

handleOpen(res){
      window.open("details.html");
      console.log(datas);//这里可以拿到点击的数据但是怎么传给新开页面的组件呢?
  },
  render() {
    return (
    <div className="photo">
      <h2 className="lastest-title-photo">最新作品
      </h2>
      <Row className="skill">
           {
          this.state.data.map(function(res){
              return <Col span="24" key={res.ID} ref="myTextInput">
                <div className="slil-card" >
                  <div className="skill-img" onClick={this.handleOpen.bind(this,res)}>
                    <img src={res.URL} />
                  </div>
                  <div className="skill-content">
                    <h2 className="skill-title"><span onClick={this.handleOpen.bind(this,res)}>{res.TITLE}</span></h2>
                    <p className="skill-subtext">{res.SUBTEXT}</p>
                  </div>
                </div>             
                 </Col>
          },this)
        } 
      </Row>
    </div>
    );

我想要在另一个jsx文件中得到我所点击元素的数据该怎么做?

 import ReactDOM from 'react-dom';
import React from 'react';
import { render } from 'react-dom';
import Skill from '../component/Skill';
import '../details/all.less';
const App = React.createClass({
  getInitialState: function() {
    //怎么获取到点击元素的数据?
      
  },
  render() {
      
    return (
      <div>
        <div className="details-head">
            <img src=""/>
        </div>
      </div>
    )
  }
});

ReactDOM.render(<App />, document.getElementById('web-main1'));
阅读 2.8k
2 个回答

两个思路,第一个:

  1. 构建父子组件,通过父来当数据中心,子分别从其中取值

  2. 使用或自己定义数据中心,如redux,localstorage等

非要用window.open吗?
window.open 会打开一个新页面,我感觉不符合单页的意义

真的要用的话,我感觉只能用windows.open('http://xxx.xxx.xx/some.html?arg1=something&arg2=something);

然后在racet-router中配置,在页面中通的props.params的来获得 arg1,arg2的内容

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