反应更改页面标题

时间:2018-06-28 16:35:30

标签: react-router react-helmet

我知道我可以使用react-document-titlereact-helmet来更改页面标题。但是我有一个问题。

react-document-title可以设置默认页面标题,例如:

str1 = "Is Python a better first language to learn than Java"
str2 = "Which of Python or Java is a better programming language to 
start out with"

tf_sim(str1, str2)

反应头盔能做同样的事情吗?

此外,从github中的示例中,他们两个都使用静态标题。他们可以像youtube这样的标题不是静态的吗?

如果您位于youtube主页上,则页面标题显示为Youtube。
如果您正在观看视频,则页面标题会以 Traceback (most recent call last): File "SimilarityCalculator.py", line 143, in <module> sc.tf_sim(str1, str2) File "SimilarityCalculator.py", line 67, in tf_sim print (sim_scores.eval()) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 710, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5180, in _eval_using_default_session return session.run(tensors, feed_dict) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 900, in run run_metadata_ptr) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1135, in _run feed_dict_tensor, options, run_metadata) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1316, in _do_run run_metadata) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1335, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'sim_input1' with dtype string [[Node: sim_input1 = Placeholder[dtype=DT_STRING, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] Caused by op 'sim_input1', defined at: File "SimilarityCalculator.py", line 143, in <module> sc.tf_sim(str1, str2) File "SimilarityCalculator.py", line 40, in tf_sim sim_input1 = tf.placeholder(tf.string, shape=(None), name="sim_input1") File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1808, in placeholder return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 4848, in placeholder "Placeholder", dtype=dtype, shape=shape, name=name) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper op_def=op_def) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3392, in create_op op_def=op_def) File "/Users/kaushik.visvanathan/anaconda3/envs/Community/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1718, in __init__ self._traceback = self._graph._extract_stack() # pylint: disable=protected-access InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'sim_input1' with dtype string [[Node: sim_input1 = Placeholder[dtype=DT_STRING, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] 的形式显示视频的名称。

很显然,function App() { // Use "My Web App" if no child overrides this return ( <DocumentTitle title='My Web App'> <SomeRouter /> </DocumentTitle> ); } 是静态的,视频的名称是动态的。

目标是我要在router.js中设置默认标题(如-youtube),然后如果组件需要标题(默认标题加上组件的页面标题),请更改页面标题。如果不需要,请使用以下代码使用默认标题:

- youtube

您可以显示示例,因为我需要它来理解它?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用document.title来更改页面标题,如下所示。

import React from 'react';

const DemoComponent= () => {
  document.title = "This is demo Component";
  return (
    <div className="container">
      <div className="row">
        <h1> My Component</h1>
      </div>
    </div>
  );
}

export default DemoComponent;

在这里,我设置了页面“这是演示组件”的标题。因此,呈现此组件时,页面标题将被更改。