将内容从 html 转换为 ReactJS 文件

Converting contents from html to ReactJS file

如果问题令人困惑,我们深表歉意。基本上我有这个 html 代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction</title>

    <script
      src="https://rawgit.com/flatiron/director/master/build/director.min.js">
    </script>

    <script>
      var author = function () { console.log("author"); };
      var books = function () { console.log("books"); };
      var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
      };

      var routes = {
        '/author': author,
        '/books': [books, function() {
          console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
      };

      var router = Router(routes);

      router.init();
    </script>
  </head>

  <body>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
      <li><a href="#/books/view/1">#/books/view/1</a></li>
    </ul>
  </body>
</html>

这显然在 .html 文件中。我想把它改成一个.js文件,这样我就可以把html放在js里面,这样当点击不同的链接时,routed/returned是不同的。

我真的不知道如何直接将其放入 javascript 文件,然后让路由器工作。这是 html 文件来自 https://github.com/flatiron/director#client-side-routing 的地方,我正在尝试使用这个 flatiron/director 路由器。

任何帮助都会很棒!

我能够让它与 React 和 jsx 以及外部的路由代码一起工作。

写成es6/es2015

app.js

const author = () => { console.log("author"); };
const books = () => { console.log("books"); };
const viewBook = (bookId) => { console.log("viewBook: bookId is populated: " + bookId); };

const routes = {
  '/author': author,
  '/books': [books, () => { console.log("An inline route handler."); }],
  '/books/view/:bookId': viewBook
};

const router = Router(routes);

router.init();

class SampleRouting extends React.Component {
  render() {
    return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      </ul>
    )
  }
}

React.render( <SampleRouting/> , document.getElementById('root'));

index.html

<div id="root"></div>

样本: http://s.codepen.io/oobgam/debug/vNoogO

_edited app.js 以反映状态和页眉的更新

class App extends React.Component {
 constructor(props) {
    super(props);
    this.state = { currentPage: 'author' }
 }

  componentDidMount() {
    const author = () => { this.setState({currentPage: 'author'}) };
    const books = () => { this.setState({currentPage: 'Books'}); };
    const viewBook = (bookId) => { this.setState({currentPage: 'Book ' + bookId }); };
    const routes = {
      '/author': author,
      '/books': books,
      '/books/view/:bookId': viewBook
    };
    const router = Router(routes);
    router.init();
  }

  render() {
    return (
      <div>
        <h1>{ this.state.currentPage }</h1>
        <SampleRouting />
      </div>
    );
  }
}

// stateless function
const SampleRouting = () => {
   return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      < /ul>
    )
}