反应路线问题组件

时间:2017-02-05 12:10:52

标签: javascript reactjs react-native react-router

这是我的app.js

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, IndexRoute, browserHistory } from 'react-router'

const App = ({ children }) => (
  <div>
    {children}
  </div>
)

import Index from './index'
import Eng from './wEng'
import Ar from './wAr'

render((
  <Router history={browserHistory}>
    <Route path="/simply" component={App}>
      <IndexRoute component={Index}/>
      <Route path="/A" component={Ar}/>
      <Route path="/e" component={Eng}/>
    </Route>
  </Router>
), document.getElementById('app'))

这是我的index.js

import React  from 'react';
import ReactDOM from 'react-dom';
import $ from "min-jquery";
import axios from "axios";
import { render } from 'react-dom'
import WEng from './wEng'
import WAr from './wAr'
import {Link} from 'react-router';


const urlP=`http://localhost:3000/blah`;

export default class App extends React.Component {
constructor(props){
    super(props);
    this.state={ 
      imagedayA:[],
      imagenightA:[]
      };
  }
   componentDidMount() {
     axios.get(urlP)
            .then(function (response) {
              console.log(response.data,"this is response")
              this.setState({
                imagedayA:response.data.today.iconday,
                imagenightA:response.data.today.iconnight
              })
          }.bind(this))
            .catch(function (response) {
              console.log(response);
          });
      }
  render(){
    if(this.state.component) {
      return this.state.component;
    } 

    return (
       <div>
          <span><button className=" button1" onClick={() => this.setState({component: <WAr {...this.state} />})}>Ar</button></span>
          <span><button className=" button2" onClick={() => this.setState({component: <WEng {...this.state} />})}>Eng</button></span>
        </div>
    );
  };
}

这是wAr组件:

import React from 'react'
import WEng from './wEng'
import Index from './index'

export default class App extends React.Component {
  render() {
    return <div>
  <div className="mainAr">          
          <img className="today_imgAr" src={this.props.imagedayA}/>
          <p className="tempdayAar">{this.props.tempdayA}<span className="degree">&deg;</span></p>
     </div>     

   </div>
  }
}

我想在wAr组件中添加一个按钮,所以当我点击它时,请将我从wAr组件带到WEng组件,然后使用react进行操作。

1 个答案:

答案 0 :(得分:0)

您需要使用Link

中的react-router
import React from 'react';
import {Link} from 'react-router';
import WEng from './wEng'
import Index from './index'

export default class App extends React.Component {
  render() {
    return <div>
  <div className="mainAr">          
          <img className="today_imgAr" src={this.props.imagedayA}/>
          <p className="tempdayAar">{this.props.tempdayA}<span className="degree">&deg;</span></p>
     </div>     
     <button><Link to="/e">Navigate To E</Link></button>
   </div>
  }
}
相关问题